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 ...
随机推荐
- 从Evernote大批顶尖高管离职,看处于漩涡中的笔记应用未来前景
无论是巨头,还是独角兽,甚至是小而美的某些企业,在发生高管离职.裁员等情况时,总会引起业界的广泛关注.究其原因,就在于高管离职.裁员等往往意味着企业内部发生了动荡,甚至还会直接反映出所在行业的发展趋势 ...
- UML的用例图
1.概念理解 (1)用例图是UML多种图形语言的一种,最能体现系统结构,直观展现系统功能模块模型 (2)用例图用于描述用户与用例(角色与功能模块)之间的关联关系 (3)常用Power Designer ...
- js 动画滚动到指定位置 ES6
### 开始 ### 写一个自动滚动过度到指定位置的一个函数 通过Class进行封装 /** * 滚动动画过度 * @param {Object} position 定位(只支持Y轴) * @para ...
- 树状数组--模版1和2 P3368、P3374
题目描述 如题,已知一个数列,你需要进行下面两种操作: 将某一个数加上 x 求出某区间每一个数的和 输入格式 第一行包含两个正整数 n,m,分别表示该数列数字的个数和操作的总个数. 第二行包含 n 个 ...
- leetcode中的sql
1 组合两张表 组合两张表, 题目很简单, 主要考察JOIN语法的使用.唯一需要注意的一点, 是题目中的这句话, "无论 person 是否有地址信息".说明即使Person表, ...
- python图像处理常用方法
在线标注网站 https://gitlab.com/vgg/via http://www.robots.ox.ac.uk/~vgg/software/via/via.html 数组与图像互转 from ...
- 剑指offer【10】- 变态跳台阶
题目:一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 关于本题,前提是n个台阶会有一次n阶的跳法.分析如下: f(1) = 1 f(2) ...
- R 画地图
R 绘制中国省市分布地图 library(maps) library(mapdata) library(maptools); getColor=function(mapdata,provname,pr ...
- ansible批量部署模块(二)
回顾:Ansible: 无需客户端程序 只要有SSH 模块化 ansible帮助工具ansible-doc 模块名ansible-doc 模块名 -s 列出该模块的所有选项ansible-doc -l ...
- raw_input|active:|continue|break|
a = "please" b = "say something:" c =a+b m = 0 a = True while a: m = int(raw_inp ...