Apache HttpClient 4.1-代理设置

我正在尝试将一些参数发布到服务器,但是我需要设置代理。您可以帮助我对代码的“设置代理”部分进行排序吗?

HttpHost proxy = new HttpHost("xx.x.x.xx");

DefaultHttpClient httpclient = new DefaultHttpClient();

httpclient.getParams().setParameter("3128",proxy);

HttpPost httpost = new HttpPost(url);

List<NameValuePair> nvps = new ArrayList<NameValuePair>();

nvps.add(new BasicNameValuePair("aranan", song));

httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

HttpResponse response = httpclient.execute(httpost);

HttpEntity entity = response.getEntity();

System.out.println("Request Handled?: " + response.getStatusLine());

in = entity.getContent();

httpclient.getConnectionManager().shutdown();

回答:

是的,这是我自己解决的问题

httpclient.getParams().setParameter("3128",proxy);

应该

httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);

HttpHost proxy = new HttpHost("ip address",port number);

DefaultHttpClient httpclient = new DefaultHttpClient();

httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);

HttpPost httpost = new HttpPost(url);

List<NameValuePair> nvps = new ArrayList<NameValuePair>();

nvps.add(new BasicNameValuePair("param name", param));

httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.ISO_8859_1));

HttpResponse response = httpclient.execute(httpost);

HttpEntity entity = response.getEntity();

System.out.println("Request Handled?: " + response.getStatusLine());

InputStream in = entity.getContent();

httpclient.getConnectionManager().shutdown();

以上是 Apache HttpClient 4.1-代理设置 的全部内容, 来源链接: utcz.com/qa/418041.html

回到顶部