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包如下图:

示例代码:
- 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.baidu.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://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();
- }
- }
HttpClient之Get请求和Post请求示例的更多相关文章
- httpclient就是个能发送http连接的工具包,包括能发送post请求和get请求
1.httpclient就是个能发送http连接的工具包,包括能发送post请求和get请求. http 连接一次就有返回流.http是个双向的嘛.只有连接了,就会有输出返回流. 所以在执行http连 ...
- 03_Django-GET请求和POST请求-设计模式及模板层
03_Django-GET请求和POST请求-设计模式及模板层 视频:https://www.bilibili.com/video/BV1vK4y1o7jH 博客:https://blog.csdn. ...
- Ajax中get请求和post请求
我们在使用Ajax向服务器发送数据时,可以采用Get方式请求服务器,也可以使用Post方式请求服务器,那么什么时候该采用Get方式,什么时候该采用Post方式呢? Get请求和Post请求的区别: 1 ...
- slave IO流程之二:注册slave请求和dump请求
slave IO流程已经在http://www.cnblogs.com/onlyac/p/5815566.html中有介绍 这次我们要探索注册slave请求和dump请求的报文格式和主要流程. 一.注 ...
- loadrunner录制脚本如何选择使用get请求和post请求的方式
在loadrunner工具里录制脚本时常常会用到get请求和post请求,有关loadrunner常用的这两类的请求主要有: get请求: web_url 和 web_link post请求: web ...
- iOS开发网络篇—GET请求和POST请求
iOS开发网络篇—GET请求和POST请求 一.GET请求和POST请求简单说明 创建GET请求 // 1.设置请求路径 NSString *urlStr=[NSString stringWithFo ...
- 普通请求和ajax请求的区别
普通请求和ajax请求的区别? 下面的action返回一个json文件,文件内容为sts.*,data1
- iOS开发网络篇—GET请求和POST请求(转)
一.GET请求和POST请求简单说明 创建GET请求 1 // 1.设置请求路径 2 NSString *urlStr=[NSString stringWithFormat:@"http:/ ...
- GET请求和POST请求的区别
request获取请求参数 最为常见的客户端传递参数方式有两种: 浏览器地址栏直接输入:一定是GET请求: 超链接:一定是GET请求: 表单:可以是GET,也可以是POST,这取决与<form& ...
随机推荐
- Fiddler2 下断点修改HTTP报文
一 Fiddler中设置断点修改HTTP请求 方法1:全局断点.Rules-->Automatic BreakPoint-->Before Requests(或快捷键F11),这种方法会拦 ...
- dfs 例题皇后问题
题目描述 一个如下的 6 \times 66×6 的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 上面的布局可以用序列 ...
- 洛谷 P2568 GCD 题解
原题链接 庆祝一下:数论紫题达成成就! 第一道数论紫题.写个题解庆祝一下吧. 简要题意:求 \[\sum_{i=1}^n \sum_{j=1}^n [gcd(i,j)==p] \] 其中 \(p\) ...
- Java 入门学习知识点整理
[JAVA一个文件写多个类 ( 同级类 ) 规则和注意点] 在一个.java文件中可以有多个同级类, 其修饰符只可以public/abstract/final/和无修饰符 public修饰的只能有一 ...
- angular中$q用法, $q多个promise串行/同步/等待), $q.all用法,使用
$q的基本用法 function fn() { var defer = $q.defer(); setTimeout(function () { console.log(1); defer.resol ...
- command > /dev/null command > /dev/null 2>&1nohup command &> /dev/null的区别
1.对以下命令进行依次区分 command 执行一条普通的命令 command > /dev/null '>'表示将标准输出重定向 '>>'表示追加,/dev/null是一 ...
- spring-cloud-gateway过滤器实践
概述 这里是 SpringCloud Gateway 实践的第一篇,主要讲过滤器的相关实现.Spring-Cloud-Gateway 是以 WebFlux 为基础的响应式架构设计, 是异步非阻塞式的, ...
- 走近源码:Redis如何清除过期key
"叮--",美好的周六就这么被一阵钉钉消息吵醒了. 业务组的同学告诉我说很多用户的帐号今天被强制下线.我们的帐号系统正常的逻辑是用户登录一次后,token的有效期可以维持一天的时间 ...
- 理解BERT:一个突破性NLP框架的综合指南
概述 Google的BERT改变了自然语言处理(NLP)的格局 了解BERT是什么,它如何工作以及产生的影响等 我们还将在Python中实现BERT,为你提供动手学习的经验 BERT简介 想象一下-- ...
- Arcgis License的安装及破解
1.双击LicenseManager安装目录下的Setup.exe. 2.点击“Next”. 3.选择“I accept the license agreement”,点击“Next”. 4.点击“C ...