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 ...
随机推荐
- 动态树之link-cut tree
说好的专题... lct的一些概念看论文 杨哲<QTREE解法的一些研究> 简单易懂. 首先不要把lct想象得很难,其实很水的.lct就是很多splay树维护的树... lct的acces ...
- 使用Python中的urlparse、urllib抓取和解析网页(一)(转)
对搜索引擎.文件索引.文档转换.数据检索.站点备份或迁移等应用程序来说,经常用到对网页(即HTML文件)的解析处理.事实上,通过Python 语言提供的各种模块,我们无需借助Web服务器或者Web浏览 ...
- MySQL的show语句大全
常用的MySQL show 语句列举如下: 1.show databases ; // 显示mysql中所有数据库的名称 2.show tables [from database_name]; // ...
- WPF 傻瓜生成 .dbml文件,以及文件用途原理是什么
- Export-XLSX PowerShell generate real Excel XLSX files without Excel and COM
http://gallery.technet.microsoft.com/scriptcenter/Export-XLSX-PowerShell-f2f0c035
- json解析json字符串时候,数组必须对应jsonObjectArray,不能对应JsonObject。否则会解析错误。
json第三方解析json字符串时候,json数组必须对应jsonObjectArray,不能对应JsonObject.->只要是[]开头的都是json数组字符串,就要用jsonArray解析 ...
- js获取各种宽高方法
屏幕的有效宽高: window.screen.availHeightwindow.screen.availWidth 网页可见区域宽:document.body.clientWidth 网页可见区域高 ...
- Apache服务器安装配置
Apache服务器安装 1.Apache服务器安装 在Linux系统下,apache服务器的安装方式比较灵活,可以使用二进制包安装,比如:rpm包.deb包.已编译好的包.也可以简单的使用y ...
- Delphi 记录类型- 结构指针
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- ②springMVC入门
1 1.1 需求 以案例作为驱动. springmvc和mybaits使用一个案例(商品订单管理). 功能需求:商品列表查询 1.2 环境准备 数据库环境:mysql5.1