java 后台构造http转发请求
来源:互联网 发布:ea自动交易软件 编辑:程序博客网 时间:2024/05/29 16:28
public void request(String requestUri, String msg) throws IOException
{
// 设置请求参数
String url = requestUri;
// 1、构造http请求包
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 1000 * 60 * 60);
HttpConnectionParams.setSoTimeout(httpParams, 1000 * 60 * 60);
HttpClient httpClient = new DefaultHttpClient(httpParams);
HttpHost target = new HttpHost(HOST_NAME, HTTP_PORT, "http");
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Content-type", "application/x-www-form-urlencoded"); // 必须设置成该方式
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(REQ_MSG_NAME, msg));
httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); // UrlEncodedFormEntity
// 2、发送http请求
HttpResponse response = httpClient.execute(target, httppost);
// 3、响应校验
HttpEntity resEntity = response.getEntity();
if (HttpURLConnection.HTTP_OK == response.getStatusLine()
.getStatusCode())
{
GeneralHttpResponse result = null;
if (resEntity != null)
{
BufferedReader br = new BufferedReader(new InputStreamReader(
resEntity.getContent()));
String rt = "";
String line = "";
while ((line = br.readLine()) != null)
{
rt = rt + "\r\n" + line;
}
rt = new String(rt.getBytes(), HTTP.UTF_8);
result = new GeneralHttpResponse(rt);
br.close();
EntityUtils.consume(resEntity);
}
if (!result.isSuccess())
{
throw new RuntimeException(
"Rquest client info failed on url [ " + url
+ "], " + result.getErrors());
}
}
else
{
throw new IOException("Communication with uri: "
+ response.getStatusLine());
}
}
{
// 设置请求参数
String url = requestUri;
// 1、构造http请求包
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 1000 * 60 * 60);
HttpConnectionParams.setSoTimeout(httpParams, 1000 * 60 * 60);
HttpClient httpClient = new DefaultHttpClient(httpParams);
HttpHost target = new HttpHost(HOST_NAME, HTTP_PORT, "http");
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Content-type", "application/x-www-form-urlencoded"); // 必须设置成该方式
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(REQ_MSG_NAME, msg));
httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); // UrlEncodedFormEntity
// 2、发送http请求
HttpResponse response = httpClient.execute(target, httppost);
// 3、响应校验
HttpEntity resEntity = response.getEntity();
if (HttpURLConnection.HTTP_OK == response.getStatusLine()
.getStatusCode())
{
GeneralHttpResponse result = null;
if (resEntity != null)
{
BufferedReader br = new BufferedReader(new InputStreamReader(
resEntity.getContent()));
String rt = "";
String line = "";
while ((line = br.readLine()) != null)
{
rt = rt + "\r\n" + line;
}
rt = new String(rt.getBytes(), HTTP.UTF_8);
result = new GeneralHttpResponse(rt);
br.close();
EntityUtils.consume(resEntity);
}
if (!result.isSuccess())
{
throw new RuntimeException(
"Rquest client info failed on url [ " + url
+ "], " + result.getErrors());
}
}
else
{
throw new IOException("Communication with uri: "
+ response.getStatusLine());
}
}
0 0
- java 后台构造http转发请求
- java web http请求转发
- java构造http请求
- java 后台发送http请求
- http 请求转发服务器
- java使用NIO构造http请求
- HTTP 请求转发、重定向
- 后台发送Http请求
- Fiddler 构造http请求
- java后台发送http请求(post,get)
- 获取http协议请求行、请求头、请求转发
- 后台发送http请求 类
- C#后台发送Http请求
- C#后台发送HTTP请求
- HTTP 请求转发、重定向和包含
- HTTP 重定向与请求转发
- http重定向与请求转发
- jsp web服务器转发http请求
- hadoop伪分布模式安装步骤
- Struts 登陆
- 世界杯主题曲 由我们主宰
- Struts session
- HttpAsyncClient 介绍
- java 后台构造http转发请求
- Struts2 之addActionError的使用
- Linux网卡绑定探析
- centos6.5 x86_64安装oracle 11.2.0.3grid
- 一个屌丝程序员对项目测试一些想法
- JavaScript实现下拉菜单(鼠标、键盘操作)
- silent install oracle 11.2.0.1 x86_64 for linux
- android 异步任务的用法详解
- mysql忘记密码怎么办?