⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplehttpclient.java

📁 用来为垂直搜索引擎抓取数据的采集系统
💻 JAVA
字号:
package org.indigo.tests;

import java.io.IOException;



import org.apache.commons.httpclient.*;

import org.apache.commons.httpclient.methods.*;
import org.indigo.parser.Parser;

/**

 * 提交参数演示

 * 该程序连接到一个用于查询手机号码所属地的页面

 * 以便查询号码段1330227所在的省份以及城市

 * @author Liudong

 */

public class SimpleHttpClient {

 

    public static void main(String[] args) throws IOException

    {

        HttpClient client = new HttpClient();

 //       client.getHostConfiguration().setHost("www.scnjw.gov.cn/", 80, "http");

 

        HttpMethod method = getPostMethod();//使用POST方式提交数据

        client.executeMethod(method);

       //打印服务器返回的状态

        System.out.println(method.getStatusLine());

        //打印结果页面

        String response =

           new String(method.getResponseBodyAsString().getBytes("gb2312"));

       //打印返回的信息

        System.out.println(response);

        method.releaseConnection();

    }

    /**

     * 使用GET方式提交数据

     * @return

     */

    private static HttpMethod getGetMethod(){

        return new GetMethod("/simcard.php?simcard=1330227");

    }

    /**

     * 使用POST方式提交数据

     * @return

     */

    private static HttpMethod getPostMethod(){

        PostMethod post = new PostMethod("http://www.scnjw.gov.cn/schq/schq.aspx");

        Parser parser=new Parser();
		parser.setUrl("http://www.scnjw.gov.cn/schq/schq.aspx");
		parser.open();
		String middle=parser.parseWith("<input type=\"hidden\" name=\"__VIEWSTATE\" value=\"", "\" />");
       System.out.println(middle);
		NameValuePair[] data = { new NameValuePair("__EVENTTARGET", "TbGrid1:DataGrid1:_ctl39:_ctl0")
        ,new NameValuePair("__EVENTARGUMENT", " "),
        new NameValuePair("__VIEWSTATE",middle) };
     
        post.setRequestBody(data);

        return post;

    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -