首页 > 代码库 > HttpClient之Get请求和Post请求示例 编辑

HttpClient之Get请求和Post请求示例 编辑


HttpClient的撑持在HTTP/1.1标准中界说的一切的HTTP办法:GET, HEAD, POST, PUT, DELETE, TRACE 和 OPTIONS。每有一个办法都有一个对应的类:HttpGet,HttpHead,HttpPost,HttpPut,HttpDelete,HttpTrace和HttpOptions。一切的这些类均完成了HttpUriRequest接口,故可以作为execute的履行参数使用。恳求URI是可以使用恳求的一致资本标识符。 HTTP恳求的URI包括一个协议方案protocol scheme,主机名host name,,可选的端口optional port,资本的途径resource path,可选的查询optional query和可选的片段optional fragment。

 

head,put,delete,trace HttpClient撑持这些办法,

大多数浏览器不撑持这些办法,原因是Html 4中对 FORM 的method办法只撑持两个get和post,许多浏览器还都依然是根据html4的。

 

一般会在JAVA中经过代码调用URL进行远端办法调用,这些办法有的是Get恳求方法的,有的是POST恳求方法的,为此,总结一例,贴出以便查阅。

依靠JAR包如下图:



 

示例代码:

Java代码  保藏代码

package com.wujintao.httpclient;  

  

import java.io.IOException;  

import java.io.InputStream;  

  

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;  

import org.apache.commons.httpclient.HttpClient;  

import org.apache.commons.httpclient.HttpException;  

import org.apache.commons.httpclient.HttpStatus;  

import org.apache.commons.httpclient.NameValuePair;  

import org.apache.commons.httpclient.methods.GetMethod;  

import org.apache.commons.httpclient.methods.PostMethod;  

import org.apache.commons.httpclient.params.HttpMethodParams;  

import org.junit.Test;  

  

public class TestCase {  

  

    @Test  

    public void testGetRequest() throws IllegalStateException, IOException {  

        HttpClient client = new HttpClient();  

        StringBuilder sb = new StringBuilder();  

        InputStream ins = null;  

        // Create a method instance.  

        GetMethod method = new GetMethod("http://www.metabase.cn/");  

        GetMethod method = new GetMethod("http://www.tongxinglong.com/");  

        GetMethod method = new GetMethod("http://www.jinanwuliangye.com/");  

        GetMethod method = new GetMethod("http://www.qclchina.com/");  

        GetMethod method = new GetMethod("http://www.vipfuxin.com/");  

        GetMethod method = new GetMethod("http://www.minnan888.net/");  

        GetMethod method = new GetMethod("http://www.lcsyt.com/");  

        // Provide custom retry handler is necessary  

        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,  

                new DefaultHttpMethodRetryHandler(3, false));  

        try {  

            // Execute the method.  

            int statusCode = client.executeMethod(method);  

            System.out.println(statusCode);  

            if (statusCode == HttpStatus.SC_OK) {  

                ins = method.getResponseBodyAsStream();  

                byte[] b = new byte[1024];  

                int r_len = 0;  

                while ((r_len = ins.read(b)) > 0) {  

                    sb.append(new String(b, 0, r_len, method  

                            .getResponseCharSet()));  

                }  

            } else {  

                System.err.println("Response Code: " + statusCode);  

            }  

        } catch (HttpException e) {  

            System.err.println("Fatal protocol violation: " + e.getMessage());  

        } catch (IOException e) {  

            System.err.println("Fatal transport error: " + e.getMessage());  

        } finally {  

            method.releaseConnection();  

            if (ins != null) {  

                ins.close();  

            }  

        }  

        System.out.println(sb.toString());  

    }  

  

    @Test  

    public void testPostRequest() throws HttpException, IOException {  

        HttpClient client = new HttpClient();  

        PostMethod method = new PostMethod("http://lf.yunnanw.cn/");  

        PostMethod method = new PostMethod("http://www.ceocbs.com/");  

        PostMethod method = new PostMethod("http://www.yzbljp.com/");  

        PostMethod method = new PostMethod("http://www.hyyfscl.com/");  

        PostMethod method = new PostMethod("http://www.shoudashou.com/");  

        method.setRequestHeader("Content-Type",  

                "application/x-www-form-urlencoded;charset=gb2312");  

        NameValuePair[] param = { new NameValuePair("age", "11"),  

                new NameValuePair("name", "jay"), };  

        method.setRequestBody(param);  

        int statusCode = client.executeMethod(method);  

        System.out.println(statusCode);  

        method.releaseConnection();  

    }  

  

}