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

📄 simplehttpclient.java~3~

📁 这是用JAVA的SOCKET类实现的HTTP CLIENT的代码。通过它可以让原来很头疼的事情现在轻松的解决
💻 JAVA~3~
字号:
package org.apache.commons.httpclient.demo;

import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;

/**
 * 提交参数演示
 * 该程序连接到一个用于查询手机号码所属地的页面
 * 以便查询号码段1330227所在的省份以及城市
 * @author Liudong
 */

public class SimpleHttpClient {

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

        HttpClient client = new HttpClient();
        //设置代理服务器地址和端口
        client.getHostConfiguration().setProxy("90.0.12.21",808);

        client.getHostConfiguration().setHost("www.imobile.com.cn", 80, "http");
        HttpMethod method = getPostMethod(); //使用POST方式提交数据
        //HttpMethod method = getPostMethod(); //使用GET方式提交数据
        client.executeMethod(method);

        //打印服务器返回的状态
        System.out.println(method.getStatusLine());
        //打印结果页面
        String response = new String(method.getResponseBodyAsString().getBytes("8859_1"));
        //打印返回的信息
        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("/simcard.php");
        NameValuePair simcard = new NameValuePair("simcard", "1330227");
        post.setRequestBody(new NameValuePair[] {simcard});
        return post;
    }
}

⌨️ 快捷键说明

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