HttpClient GET和POST请求
package com.rogue.hclient; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity; /**
* 测试HttpClient功能
* @author djoker
*
*/
public class HClientTest { HttpClient client = new HttpClient(); //get功能测试
public void getTest(){
String uri = "http://172.16.100.20/cgi-bin/ht.cgi?method=getMethodTest";
GetMethod method = new GetMethod(uri);
try {
int code = client.executeMethod(method);
System.out.println(code);
if(200 == code){ // StringBuffer sb = new StringBuffer();
// sb.append(method.getResponseBodyAsString()); //不推荐使用,会有警告,如果读取的内容过多,会导致超过最大读取值
// System.out.println(sb.toString()); InputStream is = method.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
}
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //POST测试
public void postTest(){
String uri = "http://172.16.100.20/cgi-bin/ht.cgi";
String content = "method=PostMethod¶mer=paramer"; //参数
PostMethod method = new PostMethod(uri);
RequestEntity requestEntity = new StringRequestEntity(content); //字符串请求参数
method.setRequestEntity(requestEntity); //设置请求参数
try {
int code = client.executeMethod(method);
System.out.println(code);
if(200 == code){
InputStream is = method.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
}
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void main(String[] args){
HClientTest hct = new HClientTest();
hct.getTest();
System.out.println("--------");
hct.postTest();
}
}
HttpClient GET和POST请求的更多相关文章
- HttpClient (POST GET PUT)请求
HttpClient (POST GET PUT)请求 package com.curender.web.server.http; import java.io.IOException; import ...
- HttpClient方式模拟http请求设置头
关于HttpClient方式模拟http请求,请求头以及其他参数的设置. 本文就暂时不给栗子了,当作简版参考手册吧. 发送请求是设置请求头:header HttpClient httpClient = ...
- HttpClient发送get post请求和数据解析
最近在跟app对接的时候有个业务是微信登录,在这里记录的不是如何一步步操作第三方的,因为是跟app对接,所以一部分代码不是由我写,我只负责处理数据,但是整个微信第三方的流程大致都差不多,app端说要传 ...
- HttpWebRequest 改为 HttpClient 踩坑记-请求头设置
HttpWebRequest 改为 HttpClient 踩坑记-请求头设置 Intro 这两天改了一个项目,原来的项目是.net framework 项目,里面处理 HTTP 请求使用的是 WebR ...
- 使用HttpClient发送Get/Post请求 你get了吗?
HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议 ...
- org.apache.httpcomponents httpclient 发起HTTP JSON请求
1. pom.xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactI ...
- httpclient的几种请求URL的方式
一.httpclient项目有两种使用方式.一种是commons项目,这一个就只更新到3.1版本了.现在挪到了HttpComponents子项目下了,这里重点讲解HttpComponents下面的ht ...
- HttpClient发起Http/Https请求工具类
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcl ...
- HttpClient方式模拟http请求
方式一:HttpClient import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.http.*; im ...
- Android HttpClient GET或者POST请求基本使用方法(转)
在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来方便我们使用各种Http服务.这里只介绍如何使用HttpCl ...
随机推荐
- 33. docker swarm 集群服务通信 之 RoutingMesh - Ingress 网络
1.作用 当在 任何 一个 swarm 节点去访问 端口服务的时候 会通过 本节点 的 IPVS ( ip virtual service ) 到 真正的 swarm 节点上 当访问 docker h ...
- dp--最长上升子序列LIS
1759:最长上升子序列 总时间限制: 2000ms 内存限制: 65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的.对 ...
- 68)deque数组
基本要求: 1)和vecctor基本区别 示意图 vector在尾部添加和删除, deque在尾部添加和删除,在头部添加和删除. 2)基本知识: 3)deque的构造形式: 4)基本操作和遍历 ...
- 01 语言基础+高级:1-10 JDK8新特性_day12【函数式接口】
day12[函数式接口] 主要内容自定义函数式接口函数式编程常用函数式接口 教学目标能够使用@FunctionalInterface注解能够自定义无参无返回函数式接口能够自定义有参有返回函数式接口能够 ...
- 专业程序设计part1
7 专业程序设计 (有多少人在大学里学到了自己真正喜欢的专业??并在此专业上获得了升华??)i== 软件图标下载网址:easyicon 01thu 4邻域 8邻域 D邻域 是啥 像素间的连通性,是转为 ...
- Java web:html+css(2020.1.5)
html 1.font-size中1em=16px 2.html中font不要使用 3.链接标签<a></a>禁止下划线样式设置 <style> a{ color: ...
- fread 和fgets 函数的使用
两个函数都是对FILE *fp 文件进行读取信息,fgets是每次读取一行,fread是一下子读完所有的文件内容. //一.fread的使用 FILE *fp; int nread; ] fp = f ...
- Mac 终端实现快速定位命令 自动补全目录
基于macOS oh-my-zsh 切换终端主题 incr.zsh 实现快速定位命令 自动补全目录 效果预览 步骤 1.安装 oh-my-zsh sh -c "$(curl -fsSL ht ...
- [Algo] 223. Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第四天】
https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...