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(); }
相關文章
相關標籤/搜索