http请求及模拟浏览器发送http请求
/**
*测试新增图片
* @throws IOException
* @throws HttpException
* @throws SAXException
* @throws ParserConfigurationException
*/
// @Test
// public void TestUploadImage() throws HttpException, IOException, SAXException, ParserConfigurationException{
// System. out.println("----------");
// System.out.println("----------");
// HttpClient client = new HttpClient();
// PostMethod method = new PostMethod("http://localhost:8080/egis-scms-core/order/fileUpLoad.do");
// File file = new File("D:/a.jpg");
/ / FilePart fp = new FilePart("files", file);
// StringPart barcode = new StringPart("id", "GP1101MR3401S100277736");
// StringPart picCode = new StringPart("picCode", "GP17");
// Part[] parts = {id,picCode,fp};
// MultipartRequestEntity mre = new MultipartRequestEntity(parts,method.getParams());
// method.setRequestEntity(mre);
// int httpStat = client.executeMethod(method) ;
//
// if (httpStat != 200) {
// //如果失败,获取返回XML信息
// String xml = method.getResponseBodyAsString();
// DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// DocumentBuilder builder = factory.newDocumentBuilder();
// StringReader stringReader = new StringReader(xml);
// InputSource inputSource = new InputSource(stringReader);
// Document doc = builder.parse(inputSource);
// doc.normalize();
/ / NodeList resultNL = doc.getElementsByTagName("desciption");
// String fromAgentResult = resultNL.item(0).getFirstChild().getNodeValue();
// }
// }
//开始调用服务
Map result = null;
try {
result = (Map) HttpProxy.send(scmsCoreUrl+UrlConstants.pubOrderInsuranceListUrl, params, Map.class);
} catch (IOException e) {
LoggerUtil.logError(this.getClass() .getName(), "orderList", e);
throw new BusinessException(e);
}
工具类:
public class HttpProxy {
public static Object send(String url,SettingModel setting,Map<String,String> parameter,Class<?> resultClazz){
Object result = null;
LoggerUtil.trace(HttpProxy.class.getName(), "数据接收类:" , resultClazz==null?null:resultClazz.getName()+"请求地址: " + url + "请求参数: " + parameter );
try {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy() );
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
builder.build());
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(
sslsf).build();
SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());
HttpUriRequest method = HttpMethodUtil.createMethod(url,setting,parameter,null);
HttpResponse resp = httpclient.execute(method);
String resultBody = EntityUtils.toString(resp.getEntity(), HTTP.UTF_8);
int status = resp.getStatusLine(). getStatusCode();
LoggerUtil.trace(HttpProxy.class.getName(), "返回状态:" + status,"返回值:" + resultBody);
if(resultClazz == null || resultClazz.equals(String.class)) {
result = resultBody;
}else{
Resolve resolve = ResolveFactory.createResolve(setting.getDataType());
result = resolve.resolve(resultBody, resultClazz);
}
}catch(Exception e){
LoggerUtil.logError(HttpProxy.class.getName (), resultClazz==null?"":resultClazz.getName() +"请求地址: " + url + "请求参数: " + parameter ,e );
}
return result;
}
public static Object send(String url, String data, SettingModel setting, Class<?> resultClazz) throws BusinessException {
Object result = null;
LoggerUtil.trace(HttpProxy.class.getName(), "数据接收类:" ,"请求地址: " + url + "请求参数: " + data);
try {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
builder.build());
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(
sslsf).build();
SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());
HttpUriRequest method = HttpMethodUtil.createMethod(url,setting,data,null);
HttpResponse resp = httpclient.execute(method);
String resultBody = EntityUtils.toString(resp.getEntity(), HTTP.UTF_8);
int status = resp.getStatusLine().getStatusCode();
LoggerUtil.trace(HttpProxy.class.getName(), "返回状态:" + status,"返回值:" + resultBody);
if(resultClazz == null || resultClazz.equals(String.class)){
result = resultBody;
}else{
Resolve resolve = ResolveFactory.createResolve(setting .getDataType());
result = resolve.resolve(resultBody, resultClazz);
}
}catch(Exception e){
LoggerUtil.logError(HttpProxy.class.getName(), "数据接收类:" + resultClazz==null?null :resultClazz.getName() +"请求地址: " + url + "请求参数: " + data ,e );
throw new BusinessException();
}
return result;
}
public static Object sendAddHeader(String url,SettingModel setting,Map<String,String> parameter,String Authorization,Class<?> resultClazz){ Object result = null; //StringBuilder result2 = new StringBuilder(); LoggerUtil.trace(HttpProxy. class.getName(), "数据接收类:" , resultClazz==null?null:resultClazz.getName() + "请求地址: " + url + "请求参数: " + parameter + "Auth认证密钥" + Authorization ); try { SSLContextBuilder builder = new SSLContextBuilder(); builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( builder.build()); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory( sslsf) .build();
// HttpUriRequest method = HttpMethodUtil.createMethod(url,setting,parameter,Authorization); HttpPost method = new HttpPost(url); if(Authorization != null && !"".equals(Authorization)){ method.addHeader("Authorization ", Authorization); } List<NameValuePair> param = new ArrayList<NameValuePair>(); Set<Entry<String,String>> entrySet = parameter.entrySet(); Iterator<Entry<String,String>> iterator = entrySet. iterator(); while(iterator.hasNext()){ Entry<String,String> entry = iterator.next(); param.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } method .setEntity(new UrlEncodedFormEntity(param, HTTP.UTF_8));
HttpResponse resp = httpclient.execute(method); String resultBody = EntityUtils.toString(resp.getEntity(), HTTP.UTF_8); LoggerUtil.trace(HttpProxy.class.getName(), "返回值:" , resultBody); if (resultClazz == null || resultClazz.equals(String.class)){ }else{ Resolve resolve = ResolveFactory.createResolve(setting.getDataType()); result = resolve.resolve(resultBody, resultClazz); } }catch(Exception e){ LoggerUtil.logError(HttpProxy.class.getName(), resultClazz==null?null:resultClazz.getName() +"请求地址: " + url + "请求参数: " + parameter ,e ); } return result ; }
public static Object send(String url,Map<String,String> parameter,Class<?> resultClazz) throws IOException{
SettingModel setting = new SettingModel();
return send(url,setting,parameter,resultClazz);
}
}
http请求及模拟浏览器发送http请求的更多相关文章
- java 模拟浏览器发送post请求
java使用URLConnection发送post请求 /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求 ...
- 使用HttpClient配置代理服务器模拟浏览器发送请求调用接口测试
在调用公司的某个接口时,直接通过浏览器配置代理服务器可以请求到如下数据: 请求url地址:http://wwwnei.xuebusi.com/rd-interface/getsales.jsp?cid ...
- .net后台模拟浏览器get/post请求
#region 后台模拟浏览器get/post请求 /// <summary> /// 发送请求方式 /// </summary> /// <param name=&qu ...
- 关于对浏览器发送POST请求的一点研究
网上对与HTTP的Method,GET和POST的区别,说得毕竟详细.然后提到一点,说浏览器对两者的还有一个比较容易让人忽略的区别就是:POST会分2次发送,而GET只1次. GET发送1次,这个没什 ...
- ajax是怎么发请求的和浏览器发的请求一样吗?cookie
下午设置cookie时出现了个问题 用ajax发的post请求php,在php的方法里设置了cookie,然后在浏览器请求的php里打印cookie值但是一直获取不到cookie的值 分析: 1.aj ...
- telnet客户端模拟浏览器发送请求
telnet 客户端 telnet客户端能够发出请求去连接服务器(模拟浏览器) 使用telnet之前,需要开启telnet客户端 1.进入控制面板 2.进入程序和功能,选择打开或关闭windows功能 ...
- Java基础教程——模拟浏览器发送请求
JAVA访问网页 分别测试使用get和post方法访问网页,可以收到服务器的请求,并写入到html文件中. import java.io.*; import java.net.*; import ja ...
- 20200726_java爬虫_使用HttpClient模拟浏览器发送请求
浏览器获取数据: 打开浏览器 ==> 输入网址 ==> 回车查询 ==> 返回结果 ==> 浏览器显示结果数据 HttpClient获取数据: 创建HttpClient ==& ...
- java模拟浏览器发送请求
package test; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOExcep ...
随机推荐
- HDNOIP201404最短路径
HDNOIP201404最短路径 难度级别: A: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 a.b.c是3个互不相等的1 ...
- cvReleaseImage 释放内存出错
cvReleaseImage是OpenCV中C语言库中的释放图片内存的函数,比如我们加载或者克隆了一幅图片,当不需要这幅图片了时,我们为了避免内存泄露,需要释放这些空间,可以参见我之前的博客OpenC ...
- 通过console口连接交换机
最近发现有人不会通过console口连接交换机. 想想当初我还是小白的时候也是如此啊,如是写下教程. 虽然略简单... 1.连线: console线:(Console---usb) 2.安装驱动 (可 ...
- PHP对于Session漏洞的防范
目前,基于PHP的网站开发已经成为目前网站开发的主流,本文笔者重点从PHP网站攻击与安全防范方面进行探究,旨在减少网站漏洞,希望对大家有所帮助! 一.常见PHP网站安全漏洞 对于PHP的漏洞,目前常见 ...
- htc M8 无法自动恢复数据连接(4g)的问题解决
情况如下:htc m8 tdd-lte的双待手机,4g.2g同时在线. 本月出现,在短时间没有信号的情况后,无法恢复数据连接,哪怕是edge,更不论4g了. 尝试各种方法无解.最后咨询10086解决此 ...
- 【新产品发布】【EVC8001 磁耦隔离式 USB 转 RS-485】
EVC8001 是 XiaomaGee 团队打造的精品级 USB 转 RS-485 隔离转换器,全部采用最优方案,每个细节均做到最优化.最佳化.亮点举不胜举: ==================== ...
- 【原】Windows下Nexus搭建Maven私服
一.Maven安装 详见Java开发环境搭建 二.Nexus安装 2.1.下载 地址:http://www.sonatype.org/nexus/go/ 选择OSS(ZIP)版本 2.2.安装 将安装 ...
- 【转】JavaScript顶级对象参考模型
- [IT学习]PowerBi 入门
从哪里开始呢?注册一个账号,从PowerBi的help开始就行了.Get Started会带领你从get data讲起,建立dataset,建立report,一直到dashboard创建. 下面这个链 ...
- Mininet实验 OpenFlow1.3协议基于Mininet部署与验证
参照:OpenFlow1.3协议基于Mininet部署与验证 安装过程,参考原文. 实验 使用ifconfig查看本机IP地址:192.168.1.101 进入OpenDayLight界面,cd到bi ...