httpclient 使用方式介绍
第一:Get方式请求
package com.hct; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject; public class GetPort { public static JSONObject getUrl(String url) throws ClientProtocolException, IOException {
// 创建HttpClient实例
DefaultHttpClient httpclient = new DefaultHttpClient();
JSONObject son = new JSONObject();
// 创建Get方法实例
HttpGet httpgets = new HttpGet(url);
HttpResponse response = httpclient.execute(httpgets);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instreams = entity.getContent();
String str = convertStreamToString(instreams);
son = new JSONObject(str);
System.out.println("以下是响应结果:");
System.out.println(str);
// Do not need the rest
httpgets.abort();
}
return son;
} public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder(); String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
} }
第二:post请求方式
package com.hct; import java.io.IOException;
import java.util.Map; import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject; public class PostPort { public JSONObject postResponse(String url,String json,Map<String, String> headers) throws ClientProtocolException, IOException {
HttpClient httpclient = new DefaultHttpClient();
String uri = url;
HttpPost httppost = new HttpPost(uri);
if(!headers.isEmpty()){
for(String key : headers.keySet())
{
// 遍历map对象添加http头信息
httppost.addHeader(key, headers.get(key));
}
}
JSONObject parameters = new JSONObject(json);
//将参数添加进请求url中
httppost.setEntity(new StringEntity(parameters.toString())); HttpResponse response;
//执行请求
response = httpclient.execute(httppost);
//获取状态码
int t = response.getStatusLine().getStatusCode();
if(t==200){
String rev = EntityUtils.toString(response.getEntity());
System.out.println(rev);
parameters =new JSONObject(rev);
/* 测试使用
String orderId = (String) parameters.get("orderId");
System.out.println(orderId);*/ }
return parameters;
} public JSONObject postResponse1(String url,String json) throws ClientProtocolException, IOException{
String uri =url;
String para = json;
JSONObject jso = null;
jso = postResponse(uri, para, null);
return jso; } }
第三:使用testng进行测试
package com.hct118; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import org.apache.http.client.ClientProtocolException;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test; public class TestPortCase {
//核保接口测试
@Test
public void testPost() throws ClientProtocolException, IOException {
String uri = "http://10.253.10.226:8080/invest/mvc/m/clients/underwriter";
Map<String, String> map = new HashMap<String, String>();
map.put("token", "9802525003ff40d1a39863722371f8c1");
map.put("Content-Type", "application/json");
String json = "{\"phoneNo\":18234560012,\r\n" +
"\"tradeAccount\":\"test8887\",\r\n" +
"\"name\":\"谭宝玲\",\r\n" +
"\"certNo\":\"440681198610152044\",\r\n" +
"\"amount\":\"40000.11\",\r\n" +
"\"channelRequestNo\":\"test00044442\",\r\n" +
"\"productOffingCode\":\"101001048364\"\r\n" +
"}";
PostPort pp = new PostPort();
JSONObject jso = pp.postResponse(uri, json, map);
System.out.println(jso.get("channelOrderNo"));
Assert.assertEquals(jso.get("returnMsg"), "成功");
}
@Test
public void testGet() throws ClientProtocolException, IOException
{
String url = "http://10.253.10.226:8080/invest/mvc/commons/sms/otp/new?phoneNo=18234560012&token=9802525003ff40d1a39863722371f8c1";
JSONObject js = GetPort.getUrl(url);
Assert.assertEquals(js.get("status"), "SUCCESS");
} }
httpclient 使用方式介绍的更多相关文章
- Android HttpClient HttpURLConnection相关介绍
Android HttpClient HttpURLConnection相关介绍 遇到一个问题 在android studio上用HttpClient编写网络访问代码的时候,发现该类无法导入并使用.. ...
- C#开发微信门户及应用(11)--微信菜单的多种表现方式介绍
在前面一系列文章中,我们可以看到微信自定义菜单的重要性,可以说微信公众号账号中,菜单是用户的第一印象,我们要规划好这些菜单的内容,布局等信息.根据微信菜单的定义,我们可以看到,一般菜单主要分为两种,一 ...
- **crontab的使用方式介绍和no crontab for root 提示的处理
crontab的使用方式介绍 定时任务参数详解如下: crontab -l | crontab -e www.2cto.com #*/30 * * * * ntpdate 1 ...
- Spark部署三种方式介绍:YARN模式、Standalone模式、HA模式
参考自:Spark部署三种方式介绍:YARN模式.Standalone模式.HA模式http://www.aboutyun.com/forum.php?mod=viewthread&tid=7 ...
- Mysql查看版本号的五种方式介绍
Mysql查看版本号的五种方式介绍 作者: 字体:[增加 减小] 类型:转载 时间:2013-05-03 一.使用命令行模式进入mysql会看到最开始的提示符;二.命令行中使用status可以看到 ...
- httpclient 认证方式访问http api/resutful api并获取json结果
最近,因公司线上环境rabbitmq经常发生堆积严重的现象,于是跟运维组讨论,帮助开发个集中监控所有rabbitmq服务器运行情况的应用,需要通过java访问rabbitmq暴露的http api并接 ...
- IntelliJ IDEA 编译方式介绍
编译方式介绍 相比较于 Eclipse 的实时自动编译,IntelliJ IDEA 的编译更加手动化,虽然 IntelliJ IDEA 也支持通过设置开启实时编译,但是不建议,因为太占资源了.Inte ...
- 轻量级ORM框架 QX_Frame.Bantina(二、框架使用方式介绍)
轻量级ORM框架QX_Frame.Bantina系列讲解(开源) 一.框架简介 http://www.cnblogs.com/qixiaoyizhan/p/7417467.html 二.框架使用方式介 ...
- Hive四种数据导入方式介绍
问题导读 1.从本地文件系统中通过什么命令可导入数据到Hive表? 2.什么是动态分区插入? 3.该如何实现动态分区插入? 扩展: 这里可以和Hive中的三种不同的数据导出方式介绍进行对比? Hive ...
随机推荐
- TCP/IP中链路层的附加数据(Trailer数据)和作用
1.TCP/IP中链路层的附加数据是什么 在用wireshark打开报文时,链路层显示的Trailer数据就是附加数据,如图 2.如何产生 1.例如以太网自动对小于64字节大小的报文进行填充(未实验) ...
- PHP进程通信基础——信号
PHP进程通信基础--信号 使用信号通信.可以使用kill -l 来查看当前系统的信号类型. 每个信号所代表的的详细含义,请查看我的这篇博客:http://www.cnblogs.com/roverl ...
- HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth
HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...
- Ajax方式上传文件
用到两个对象 第一个对象:FormData 第二个对象:XMLHttpRequest 目前新版的Firefox 与 Chrome 等支持HTML5的浏览器完美的支持这两个对象,但IE9尚未支持 For ...
- VBA学习
1. Range / Cells / Columns / Rows 2. 绝对引用 $F$13 / 相对引用 F13 公式所在单元格的被复制到其他位置时,绝对引用不变 3. VLookup / NLo ...
- 数据存储_FMDB
一.简单说明 1.什么是FMDB FMDB是iOS平台的SQLite数据库框架 FMDB以OC的方式封装了SQLite的C语言API 2.FMDB的优点 使用起来更加面向对象,省去了很多麻烦.冗余的C ...
- Spring Boot入门实例
简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置 ...
- HTML 内容居中方式总结
在HTML网页排版经常会用到关于对其方式的情况,水平居中和垂直居中.特别是水平居中,并不是一个简单的text-align就可以解决所有的情况. 开始之前普及一点HTML知识,目标很明显,不同的页面结构 ...
- Appcan——Box
Box架构 ub….. Box架构元素空间大小分配比例 ub-f……. Ub-f1,ub-f2,ub-f3……. Box架构元素垂直方向的位置排列 ub-ac,ub-ae… -webkit-box-a ...
- Object-C目录
Object学习目录: 1.OC概述 2.OC第一个应用程序 3.OC之类和对象(属性.方法,点语法) 4.OC之类的扩充(self,super关键字.继承.构造函数等) 5.OC之分类Categ ...