How to set proxy host on HttpClient request in Java

前端 未结 4 1165
野趣味
野趣味 2021-01-12 13:13

I want to set proxy before sending HttpClient request on a URL. As I am able to connect it curl command setting up the proxy but with Java code I am not able to

4条回答
  •  没有蜡笔的小新
    2021-01-12 14:01

    I think this could be helpful:

    HttpClient client = new HttpClient();
    
    HostConfiguration config = client.getHostConfiguration();
    config.setProxy("someProxyURL", "someProxyPort");
    
    Credentials credentials = new UsernamePasswordCredentials("username", "password");
    AuthScope authScope = new AuthScope("someProxyURL", "someProxyPort");
    client.getState().setProxyCredentials(authScope, credentials);
    
    EntityEnclosingMethod method = new PostMethod(url);
    method.setRequestEntity(new StringRequestEntity(requestXML, "text/xml", "utf-8"));
    

提交回复
热议问题