2013-08-02 31 views
1

我尝试了2个小时,无法使其工作。 这是我做过什么:如何在使用身份验证的代理服务器后面使用HTTPBuilder

  1. grails add-proxy myproxy "--host=<host>" "--port=<port>" "--username=<username>" "--password=<psw>" 
    grails use-proxy myproxy 
    

    我拒绝连接错误,指的是代理工作不

  2. 在我的常规文件,我添加代理

    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的代理,使其工作

我试图Java的方式,而不是工作

System.setProperty("http.proxyUser", username); 
    System.setProperty("http.proxyPassword", password); 

Authenticator.setDefault(new Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(username, .toCharArray()); 
    }}); 

有谁知道如何做到这一点?

回答

0

不知道是否会工作,但有some code over here,显示你应该做的:

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}" 
    } 
} 
+0

感谢,但是这是行不通的,我想这需要某事像setProxyCredentials代替。但是,我无法弄清楚API。 – user2644762

相关问题