HttpClient的例子_自己动手写网络爬虫书评-查字典图书网
查字典图书网
当前位置: 查字典 > 图书网 > > 自己动手写网络爬虫 > HttpClient的例子
lgjut 自己动手写网络爬虫 的书评 发表时间:2011-03-30 11:03:45

HttpClient的例子

书中介绍的HttpClient版本旧了,下面是新的4.0版本的例子:


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.util.ByteArrayBuffer;

public class GetAndPost {
    public static void main(String[]args){
            GetAndPost jq = new GetAndPost();
            jq.postData();
    }
    public void postData(){
            
                    // Create a new HttpClient and Post Header
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://www.lietu.com");

                    try {
                            // Add your data
                            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                            nameValuePairs.add(new BasicNameValuePair("mydata", "Mek Mek"));
                            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        
                            // Execute HTTP Post Request
                            HttpResponse response = httpclient.execute(httppost);
                            
                            InputStream is = response.getEntity().getContent();
                            BufferedInputStream bis = new BufferedInputStream(is);
                            ByteArrayBuffer baf = new ByteArrayBuffer(20);

                             int current = 0;
                             while((current = bis.read()) != -1){
                                     baf.append((byte)current);
                             }
                             
                             String text = new String(baf.toByteArray(),"utf-8"); //指定编码
                             System.out.println(text);
                             
        
                    } catch (ClientProtocolException e) {
                            // TODO Auto-generated catch block
                    } catch (IOException e) {
                            // TODO Auto-generated catch block
                    }
        }
}

谢谢支持!

展开全文
有用 4 无用 3

您对该书评有什么想说的?

发 表

推荐文章

猜你喜欢

附近的人在看

推荐阅读

拓展阅读

对“HttpClient的例子”的回应

小伟 2014-07-30 19:07:02

很感谢 有用

Sumrise 2013-10-09 17:43:16

非常棒,帮了大忙了。
这HttpClient版本不同,代码差了好多。-_-

zhouyang209117 2011-10-01 11:05:09

您好。感谢您的分享。这段代码帮我了大帮。
但有一点不足,Httpclient文档中有一句话"In order to ensure proper release of system resources one must close the content stream associated with the entity."所以应该加上is.close()这句。