httpclient 例子

来源:互联网 发布:防火墙端口开放 编辑:程序博客网 时间:2024/06/06 07:42

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;


public class Test {

 /**
  * @param args
  * @throws UnsupportedEncodingException
  */
 public static void main(String[] args) throws UnsupportedEncodingException {
  HttpClient httpclient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
   
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair("xmldata", "ss"));
    HttpPost httppost = new HttpPost("www.baidu.com");
    httppost.setEntity(new UrlEncodedFormEntity(qparams, HTTP.UTF_8));
    HttpResponse response = null;
    try {
     response = httpclient.execute(httppost, localContext);
     BufferedReader stream = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
     String l ="";
     while ((l=stream.readLine())!=null) {
      System.out.println(l);
     }
    } catch (Exception e) {
    }
    httpclient.getConnectionManager().shutdown();
   }

 

 

}

原创粉丝点击