package com.whty.sub.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
/**
* HTTP CLIENT 工具类
*
* @author cjq
*/
public class HttpClientUtil {
private static final String ENCODE_CHARSET_UTF8 = "UTF-8";
/**
*@Title: get
*@Description: TODO(模拟firefox浏览器插件poster的get方式请求)
*@Author: hxf
*@param getUrl
* 请求服务地址
*@param msTimeout
* 超时时间
*@param ip
* 被请求服务的IP地址
*@param port
* 被请求服务的端口号
*@param user
* 鉴权通过的用户名
*@param password
* 用户密码
*@return String
*/
public static String get(String getUrl,Integer msTimeout,String ip,int port,String user,String password) throws Exception{
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager ();
// 设置超时时间
connectionManager.getParams ().setConnectionTimeout (msTimeout);
HttpClient httpClient = new HttpClient (connectionManager);
// 设置被请求服务的ip和端口
httpClient.getHostConfiguration ().setHost (ip, port);
GetMethod postMethod = new GetMethod (getUrl);
httpClient.getParams ().setAuthenticationPreemptive (true);
// 设置鉴权通过的用户名和密码
httpClient.getState ().setProxyCredentials (AuthScope.ANY, new UsernamePasswordCredentials (user,password));
try {
List<Header> headers = new ArrayList<Header> ();
headers.add (new Header ("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"));
headers.add (new Header ("Authorization","Basic ZG9tYWluX3VzZXI6ZG9tYWluX3VzZXI="));
headers.add (new Header ("Accept-Encoding","gzip, deflate"));
headers.add (new Header ("Accept-Language","zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"));
headers.add (new Header ("Connection","keep-alive"));
headers.add (new Header ("User-Agent","Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1"));
httpClient.getHostConfiguration ().getParams ().setParameter ("http.default-headers", headers);
postMethod.getParams ().setParameter (HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler (1,false));
postMethod.getParams ().setParameter (HttpMethodParams.HTTP_CONTENT_CHARSET, ENCODE_CHARSET_UTF8);
postMethod.getParams ().setParameter (HttpMethodParams.HTTP_URI_CHARSET, ENCODE_CHARSET_UTF8);
postMethod.getParams ().setParameter (HttpMethodParams.SO_TIMEOUT, msTimeout);
Integer status = httpClient.executeMethod (postMethod);
if (status == 200) {
return postMethod.getResponseBodyAsString ();
} else {
throw new Exception (String.format ("Invoke remote server address %1$s error, return status = %2$s!", getUrl, status));
}
} catch (HttpException e) {
throw new Exception (String.format ("Invoke remote server address %1$s error!", getUrl),e);
} catch (IOException e) {
throw new Exception (String.format ("Invoke remote server address %1$s error!", getUrl),e);
} finally {
postMethod.releaseConnection ();
}
}
@SuppressWarnings("deprecation")
public static String post(String requeString,String postUrl,Integer msTimeout) throws Exception{
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager ();
connectionManager.getParams ().setConnectionTimeout (msTimeout);
HttpClient httpClient = new HttpClient (connectionManager);
PostMethod postMethod = new PostMethod (postUrl);
httpClient.getState ().setProxyCredentials (AuthScope.ANY, new UsernamePasswordCredentials ("domain_user","domain_user"));
try {
List<Header> headers = new ArrayList<Header> ();
headers.add (new Header ("Content-Type","text/xml; charset=" + ENCODE_CHARSET_UTF8));
httpClient.getHostConfiguration ().getParams ().setParameter ("http.default-headers", headers);
postMethod.setRequestBody (requeString);
postMethod.getParams ().setParameter (HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler (1,false));
postMethod.getParams ().setParameter (HttpMethodParams.HTTP_CONTENT_CHARSET, ENCODE_CHARSET_UTF8);
postMethod.getParams ().setParameter (HttpMethodParams.HTTP_URI_CHARSET, ENCODE_CHARSET_UTF8);
postMethod.getParams ().setParameter (HttpMethodParams.SO_TIMEOUT, msTimeout);
Integer status = httpClient.executeMethod (postMethod);
if (status == 200) {
return postMethod.getResponseBodyAsString ();
} else {
throw new Exception (String.format ("Invoke remote server address %1$s error, return status = %2$s!", postUrl, status));
}
} catch (HttpException e) {
throw new Exception (String.format ("Invoke remote server address %1$s error!", postUrl),e);
} catch (IOException e) {
throw new Exception (String.format ("Invoke remote server address %1$s error!", postUrl),e);
} finally {
postMethod.releaseConnection ();
}
}
}
分享到:
相关推荐
在开发WEB程序的时候,经常需要模拟http请求,接收服务器响应...Firefox插件poster,可以模拟各种http请求,并详细设置请求参数,比如http header,parameter等。 --2020年05月11日更新:火狐跟谷歌已经屏蔽了这个插件
poster-3.1.0-fx Poster 3.1.0.1-signed 0人参与评分 0 在用 | 0 用过 作者: Alex Milowski 适用:Firefox 1.5 - 26.* 系统:Linux, Mac, Windows ...适用于火狐43版本,截止到2016-01-18最新的poster插件
解压后可用,资源全名:Firefox Web Installer 58.0b14.exe
插件列表: Cookies Manager+ Firebug Flagfox Groundspeed hackbar 1.6.5 版本俄罗斯黑客r0oth3x49升级了很多新功能! https://addons.mozilla.org/zh-CN/firefox/addon/hackbar1/ Httpsfox JSONView Live ...
Firefox Setup 51.0b14,为什么上传这个版本,因为poster插件、json format插件在更新的版本无法安装,我得到的是这个结果。
该httpRequester适用于firefox5.6以下,可以用于http的get、poster等方式的测试,安装方法 1、安装firefox5.6以下版本 2、安装完毕,点击菜单,找到附加组件 3、在附加组件页面的搜索框左边有个按钮 4、选择 从...
WP-Poster是一款免费的WordPress文章分享海报插件,即使是小白也能够轻松使用,如果你想为网站添加一个文章分享海报功能,直接下载插件使用即可。Canvas绘图,非php后端生成,节省服务器资源,安卓系统中个别机型在...
一款好的渗透测试浏览器插件可以大大提高我们渗透测试的效率,那么作为渗透测试人员,我们是否有一些不可或缺的浏览器插件呢 ...Firefox-46.0-渗透测试版省去了我们下载插件所带来的麻烦,提高了我们的效率。
该httpRequester适用于firefox5.6以下,可以用于http的get、poster等方式的测试,安装方法 1、安装firefox5.6以下版本 2、安装完毕,点击菜单,找到附加组件 3、在附加组件页面的搜索框左边有个按钮 4、选择 从文件...
学术会议模板大量poster
火狐浏览器全插件版,hackbar,agent47,cookies manager,disable webrtc,firebug,flagfox,foxyproxy standard,groupspeed,httpfox,js switch,jsonview,live http headers,modify headers,poster,sql inject me,
之前一直找不到的插件,火狐的插件库也没搜到,下载麻烦,转存一下 最后更新时间2015年
Poster Printer即以前的ProPoster,是一款制作海报风格图片的软件,适合制作、打印大尺寸海报、标识、签名等,它支持多种图像格式,能制作最大228.6x312.97cm的大幅海报. 使用起来也非常简单,直接载入图片,然后...
.NET Framework 4 Poster
海报合成 canvas vue组件 二维码合成 页面走命令可以直接运行 合成海报 商城的海报也ok ......
Linux System Poster 2001
poster模板的合集,可以使用ppt方便地进行修改 poster模板的合集,可以使用ppt方便地进行修改 poster模板的合集,可以使用ppt方便地进行修改 poster模板的合集,可以使用ppt方便地进行修改
A1大小的poster国外好用模板,学术会议必备 A1大小的poster国外好用模板,学术会议必备 A1大小的poster国外好用模板,学术会议必备 A1大小的poster国外好用模板,学术会议必备