HttpClient3.x之Get请求和Post请求示例
来源:互联网 发布:stm32单片机引脚图 编辑:程序博客网 时间:2024/06/05 04:07
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包有:commons-codec.jar,commons-httpclient.jar,commons-logging.jar。
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 {@Testpublic 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.baidu.com");// Provide custom retry handler is necessarymethod.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());}@Testpublic void testPostRequest() throws HttpException, IOException {HttpClient client = new HttpClient();PostMethod method = new PostMethod("http://www.baidu.com/getValue");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();}}
1 0
- HttpClient3.x之Get请求和Post请求示例
- HttpClient3.x之Get请求和Post请求示例
- Java用HttpClient3发送Get和Post请求(增强)
- [Golang]Http get和post请求示例
- HttpURLConnection GET和POST请求示例
- http相应get和post请求示例
- java使用httpclient模拟post请求和get请求示例
- java发送get请求和post请求示例
- HTTPRequest 网络请求之get请求和post请求
- 请求报文之GET请求报文和POST请求报文
- get请求和post请求
- get请求和post请求
- GET请求和POST请求
- GET请求和POST请求
- GET请求和POST请求
- GET请求和POST请求
- GET请求和POST请求
- POST请求和GET请求
- 滚动数组 (定义和用法)
- 用VirtualBOx创建虚拟主机并安装XP系统教程--Windows版
- 高校平台基础系统V1.0总结
- 基于android自学开发:图片位于中间的布局(仅供参考)tcpservice.xml
- 2014年英语总结
- HttpClient3.x之Get请求和Post请求示例
- linux压缩和解压缩命令汇集
- 怎么把春节慰问信写成condolence letter呢?
- 第十八周项目三(2):使用枚举类型设计函数,可以按指定的方式输出一个平面点的对称点
- Matlab 语言
- 各种字符编码方式详解及由来
- RBAC权限管理
- QT中如何获得执行程序的路径
- RBAC权限设计实例