HttpClient 4.0 GET POST 封装

 

package org.lujian.webqq.utils;

import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;

import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
/**
*
* @author lujian
*/
public class WEB {

  HttpClient httpclient;
  HttpResponse response;

  public List<Cookie> getCookies() {
    return ((AbstractHttpClient) httpclient).getCookieStore().getCookies();
  }

  public Map<String,String> getMapCookies() {
    Map<String,String> map = new HashMap<String,String>();
    List<Cookie> cookies = ((AbstractHttpClient)httpclient).getCookieStore().getCookies();
    for (Cookie c : cookies) {
      map.put(c.getName(), c.getValue());
    }
    return map;
  }

  public String getCookie(String key) {
    for (Cookie c : ((AbstractHttpClient) httpclient).getCookieStore()
        .getCookies()) {
      if (c.getName().equals("key"))
        return c.getValue();
    }
    return null;
  }

  
  // /
  public WEB() {
    httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
        CookiePolicy.BROWSER_COMPATIBILITY);
  }

  public String get(String url) throws Exception {
    HttpGet httpget = new HttpGet(url);
    response = httpclient.execute(httpget);
    HttpEntity httpEntity = response.getEntity();
    String html = null;
    if (httpEntity != null) {
      html = EntityUtils.toString(httpEntity);
      httpEntity.consumeContent();
    }
    return html;
  }
  
  public String post(String url, List<NameValuePair> nvps) throws Exception {
    HttpPost httppost = new HttpPost(url);
    httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    response = httpclient.execute(httppost);
    HttpEntity httpEntity = response.getEntity();
    String html = null;
    if (httpEntity != null) {
      html = EntityUtils.toString(httpEntity);
      httpEntity.consumeContent();
    }
    return html;
  }

  @SuppressWarnings("unchecked")
  public String post(String url, Map map) throws IOException {
    List<NameValuePair> nvps = new ArrayList();
    Iterator it = map.entrySet().iterator();
    while (it.hasNext()) {
      Map.Entry entry = (Map.Entry) it.next();
      String key = (String) entry.getKey();
      String value = (String) entry.getValue();
      NameValuePair nvp = new BasicNameValuePair(key, value);
      nvps.add(nvp);
    }
    HttpPost httppost = new HttpPost(url);
    httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    response = httpclient.execute(httppost);
    HttpEntity httpEntity = response.getEntity();
    String html = null;
    if (httpEntity != null) {
      html = EntityUtils.toString(httpEntity);
      httpEntity.consumeContent();
    }
    return html;
  }

  public String post(String url, String str) throws Exception {
    httpclient.getConnectionManager().closeIdleConnections(30,
        TimeUnit.SECONDS);
    HttpPost httppost = new HttpPost(url);// "http://web-proxy.qq.com/conn_s"
    StringEntity reqEntity = new StringEntity(str);
    httppost.setEntity(reqEntity);
    response = httpclient.execute(httppost);
    HttpEntity httpEntity = response.getEntity();
    String html = null;
    if (httpEntity != null) {
      html = EntityUtils.toString(httpEntity);
      httpEntity.consumeContent();
    }
    return html;
  }

  public void getImg(String url, String path) throws IOException {
    HttpGet httpget = new HttpGet(url);
    response = httpclient.execute(httpget);
    HttpEntity httpEntity = response.getEntity();
    byte[] b = EntityUtils.toByteArray(httpEntity);
    File storeFile = new File(path);
    FileOutputStream output = new FileOutputStream(storeFile);
    output.write(b);
    output.close();
    if (httpEntity != null) {
      httpEntity.consumeContent();
    }
  }

  public void close() throws IOException {
    httpclient.getConnectionManager().shutdown();
  }
}

List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("url", "/home/"));
nvps.add(new BasicNameValuePair("email", user.getEmail()));
nvps.add(new BasicNameValuePair("password", user.getPassword()));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值