HttpClient4 参数传递方法

HttpClient httpclient = new DefaultHttpClient();
//设置代理服务器
HttpHost proxy = new HttpHost(Config.getProxyHost(), Config.getProxyHostPort());
//将代理服务器信息添加的httpclient  httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
HttpPost httppost = new HttpPost(Config.getLoginUrl());
//创建参数列表
List<NameValuePair> params = new ArrayList<NameValuePair>();
//开始添加参数
params.add(new BasicNameValuePair("username",Config.getLoginName()));
params.add(new BasicNameValuePair("password",Config.getPassword())); 服务器

UrlEncodedFormEntity uefEntity = null; HttpResponse response = null; HeaderElement element = null; try {         //将参数进行编码  uefEntity = new UrlEncodedFormEntity(params, "UTF-8");         //将参数添加的POST请求中  httppost.setEntity(uefEntity);  response = httpclient.execute(httppost);  Header header = response.getLastHeader("Set-Cookie");  element = header.getElements()[0];     } catch (ClientProtocolException e1) {  e1.printStackTrace(); } catch (IOException e1) {  e1.printStackTrace(); } finally {  httpclient.getConnectionManager().shutdown(); }
相关文章
相关标签/搜索