问题描述
我尝试了2个小时,但无法完成工作。这就是我所做的:
$ b $ ol <
grails add-proxy myproxy主机=<宿主gt;中--port = LT;端口> 中--username =<用户名> 中--password = LT; PSW> 中
grails使用代理myproxy
我得到了连接拒绝错误,这代表代理不工作在我的groovy文件中,我添加了代理
def http = new HTTPBuilder(http:// http://headers.jsontest.com/)
http.setProxy(host,port,http);
http.request(Method.GET,JSON){
uri.path ='/'
response.success = {resp,json - >
.....
}
}
然后我获取groovyx.net.http.HttpResponseException:需要代理验证
我无法弄清楚如何设置用户/ psw for the proxy to make work
我尝试了java方式,不工作
System.setProperty(http.proxyUser,username);
System.setProperty(http.proxyPassword,password);
和
Authenticator.setDefault(new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,.toCharArray());
}});
有谁知道如何做到这一点?
不知道它是否可行,但有一些代码是在这里显示你应该这样做:
import groovyx.net.http。*
import static groovyx.net.http.ContentType。*
import static groovyx.net.http.Method。*
import org.apache.http.auth。*
def http = new HTTPBuilder('http://www.ipchicken.com')
http.client.getCredentialsProvider()。setCredentials(
new AuthScope(myproxy.com,8080),
new UsernamePasswordCredentials(proxy-username,proxy-password)
)
http.setProxy('myproxy.com',8080,'http')
http.request(GET,TEXT){req - >
response.success = {resp,reader - >
printlnResponse:$ {reader.text}
}
}
I tried 2 hours and could not make it work.This is what I did:
grails add-proxy myproxy "--host=<host>" "--port=<port>" "--username=<username>" "--password=<psw>" grails use-proxy myproxy
I got connection refused error which mean the proxy is not working
In my groovy file, I add the proxy
def http = new HTTPBuilder("http://http://headers.jsontest.com/") http.setProxy(host, port, "http"); http.request(Method.GET, JSON) { uri.path = '/' response.success = { resp, json -> ..... } }
I then get groovyx.net.http.HttpResponseException: Proxy Authentication Required
I could not figure out how I set the user/psw for the proxy to make it work
I tried the java way, not working
System.setProperty("http.proxyUser", username);
System.setProperty("http.proxyPassword", password);
and
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, .toCharArray());
}});
Does anyone know how to do this?
Don't know if it will work, but there's some code over here that shows you should do:
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
import org.apache.http.auth.*
def http = new HTTPBuilder( 'http://www.ipchicken.com' )
http.client.getCredentialsProvider().setCredentials(
new AuthScope("myproxy.com", 8080),
new UsernamePasswordCredentials("proxy-username", "proxy-password")
)
http.setProxy('myproxy.com', 8080, 'http')
http.request( GET, TEXT ){ req ->
response.success = { resp, reader ->
println "Response: ${reader.text}"
}
}
这篇关于如何在身份验证代理的后面使用HTTPBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!