zoukankan      html  css  js  c++  java
  • 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代码  收藏代码
    1. package com.wujintao.httpclient;  
    2.   
    3. import java.io.IOException;  
    4. import java.io.InputStream;  
    5.   
    6. import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;  
    7. import org.apache.commons.httpclient.HttpClient;  
    8. import org.apache.commons.httpclient.HttpException;  
    9. import org.apache.commons.httpclient.HttpStatus;  
    10. import org.apache.commons.httpclient.NameValuePair;  
    11. import org.apache.commons.httpclient.methods.GetMethod;  
    12. import org.apache.commons.httpclient.methods.PostMethod;  
    13. import org.apache.commons.httpclient.params.HttpMethodParams;  
    14. import org.junit.Test;  
    15.   
    16. public class TestCase {  
    17.   
    18.     @Test  
    19.     public void testGetRequest() throws IllegalStateException, IOException {  
    20.         HttpClient client = new HttpClient();  
    21.         StringBuilder sb = new StringBuilder();  
    22.         InputStream ins = null;  
    23.         // Create a method instance.  
    24.         GetMethod method = new GetMethod("http://www.baidu.com");  
    25.         // Provide custom retry handler is necessary  
    26.         method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,  
    27.                 new DefaultHttpMethodRetryHandler(3, false));  
    28.         try {  
    29.             // Execute the method.  
    30.             int statusCode = client.executeMethod(method);  
    31.             System.out.println(statusCode);  
    32.             if (statusCode == HttpStatus.SC_OK) {  
    33.                 ins = method.getResponseBodyAsStream();  
    34.                 byte[] b = new byte[1024];  
    35.                 int r_len = 0;  
    36.                 while ((r_len = ins.read(b)) > 0) {  
    37.                     sb.append(new String(b, 0, r_len, method  
    38.                             .getResponseCharSet()));  
    39.                 }  
    40.             } else {  
    41.                 System.err.println("Response Code: " + statusCode);  
    42.             }  
    43.         } catch (HttpException e) {  
    44.             System.err.println("Fatal protocol violation: " + e.getMessage());  
    45.         } catch (IOException e) {  
    46.             System.err.println("Fatal transport error: " + e.getMessage());  
    47.         } finally {  
    48.             method.releaseConnection();  
    49.             if (ins != null) {  
    50.                 ins.close();  
    51.             }  
    52.         }  
    53.         System.out.println(sb.toString());  
    54.     }  
    55.   
    56.     @Test  
    57.     public void testPostRequest() throws HttpException, IOException {  
    58.         HttpClient client = new HttpClient();  
    59.         PostMethod method = new PostMethod("http://www.baidu.com/getValue");  
    60.         method.setRequestHeader("Content-Type",  
    61.                 "application/x-www-form-urlencoded;charset=gb2312");  
    62.         NameValuePair[] param = { new NameValuePair("age", "11"),  
    63.                 new NameValuePair("name", "jay"), };  
    64.         method.setRequestBody(param);  
    65.         int statusCode = client.executeMethod(method);  
    66.         System.out.println(statusCode);  
    67.         method.releaseConnection();  
    68.     }  
    69.   
    70. }  
  • 相关阅读:
    OpenLayers调用arcgis server发布的地图服务
    在线实用网址
    ArcGlobe点击IGlobeServerLayer图层读取信息
    vs2012编译出错“LC.exe”已退出解决方法
    DataTable反向模糊匹配查找语法
    PyCharm如何删除工程项目
    mysql错误日志目录
    下载HTMLTestRunner 地址
    python 单元测试之初次尝试
    cmd 运行 python
  • 原文地址:https://www.cnblogs.com/wzhanke/p/4562053.html
Copyright © 2011-2022 走看看