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 ...
随机推荐
- 过滤html字符的方法
昨天在看公司网站的产品详细页面,发现只有一个公司的JS产品轮换特效不会出,找了半天,程序都是一样的,原因是什么呢?原来是公司的别名是xiandahg,里面含有and字符被过滤掉了. public st ...
- 星外虚拟主机跨web目录文件读取漏洞
星外虚拟主机跨目录读取文件漏洞,需要一定条件. 问题发生在以下文件,这些文件都没有严格的设置执行权限,当前的IIS用户能够顺利的利用它们执行命令: c:\windows\7i24IISLOG.exe ...
- P4factory <Towards a better behavioral model: bmv2>
尝试使用bmv2行为模型来跑p4实例. 原文说明: We have released a new version of the behavioral model, written in C++. So ...
- 免费手机号码归属地API查询接口和PHP使用实例分享
免费手机号码归属地API查询接口和PHP使用实例分享 最近在做全国性的行业分类信息网站,需要用到手机号归属地显示功能,于是就穿梭于各大权威站点之间偷来了API的接口地址. 分享出来,大家可以用到就拿去 ...
- PHP Console工具使用分享
PHP Console工具使用分享 http://www.open-open.com/lib/view/open1416193590414.html 您的评价: 不错 收藏该经验 ...
- PHP CURL 多线程 GET/POST 类
PHP CURL 多线程 GET/POST 类 2015-01-01 分类:技术文章 阅读(623) 评论(0) 如果有需要更正或更高效的建议,欢迎在OSchina分享~\(≧▽≦)/~ http:/ ...
- Diode
Diode https://zh.wikipedia.org/wiki/真空管 抽真空 电子在于其放射过程中,因会与空气中之组成分子相撞而产生阻力,因此电子经由如空气之类的介质来移动的话,将会比在真空 ...
- ArcGIS Server 缓存服务切图范围
win10 + Server 10.4 + ArcMap 10.4 ArcGIS Server 缓存服务分为创建服务后手动建立缓存和创建服务时同时自动建立缓存两种. 10.2帮助文档:http:/ ...
- Lazarus如何变成XE的界面
先设置: 进入“配置安装的包”,选中 EasyDockMgr 和 easyDockMgrDsgn 这两项,重新编译 Lazarus 吧 修改以后,界面就变成了XE的了:
- jsonObject jsonarray
1.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: commons-lang.jar commons-beanutils.jar commons ...