使用HttpClient实现请求转发功能
2014-09-12 14:00
393 查看
由于移动端要显示业务系统的数据,但是业务系统众多,如果直接和业务系统交互,会紧耦合,杂乱无章,不好管理,特编写请求转发组件,实现统一中转。
本文基于HttpClient实现了GET和POST两种方式:
GET请求:
/**
* 根据GET方式请求获取请求内容
* @param url
* @return
*/
private String getContentFromUrlByGet(String url) {
String content = "";
try {
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod(url);
client.executeMethod(getMethod);
InputStream stream = getMethod.getResponseBodyAsStream();
int length;
byte[] buffer = new byte[1024 * 4];
StringBuffer stringBuffer = new StringBuffer();
while ((length = stream.read(buffer)) != -1) {
stringBuffer.append(new String(buffer, 0, length, "utf-8"));
}
content = new String(stringBuffer);
getMethod.releaseConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return content;
}
POST请求:
/**
* 根据POST请求获取请求内容
* @param url
* @return
*/
private String getContentFromUrlByPost(String url) {
String content = "";
try {
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(url);
// 增加变量
postMethod.addParameter("name", "name");
postMethod.addParameter("pwd", "pwd");
client.executeMethod(postMethod);
InputStream stream = postMethod.getResponseBodyAsStream();
int length;
byte[] buffer = new byte[1024 * 4];
StringBuffer stringBuffer = new StringBuffer();
while ((length = stream.read(buffer)) != -1) {
stringBuffer.append(new String(buffer, 0, length, "utf-8"));
}
content = new String(stringBuffer);
postMethod.releaseConnection();
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
本文基于HttpClient实现了GET和POST两种方式:
GET请求:
/**
* 根据GET方式请求获取请求内容
* @param url
* @return
*/
private String getContentFromUrlByGet(String url) {
String content = "";
try {
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod(url);
client.executeMethod(getMethod);
InputStream stream = getMethod.getResponseBodyAsStream();
int length;
byte[] buffer = new byte[1024 * 4];
StringBuffer stringBuffer = new StringBuffer();
while ((length = stream.read(buffer)) != -1) {
stringBuffer.append(new String(buffer, 0, length, "utf-8"));
}
content = new String(stringBuffer);
getMethod.releaseConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return content;
}
POST请求:
/**
* 根据POST请求获取请求内容
* @param url
* @return
*/
private String getContentFromUrlByPost(String url) {
String content = "";
try {
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(url);
// 增加变量
postMethod.addParameter("name", "name");
postMethod.addParameter("pwd", "pwd");
client.executeMethod(postMethod);
InputStream stream = postMethod.getResponseBodyAsStream();
int length;
byte[] buffer = new byte[1024 * 4];
StringBuffer stringBuffer = new StringBuffer();
while ((length = stream.read(buffer)) != -1) {
stringBuffer.append(new String(buffer, 0, length, "utf-8"));
}
content = new String(stringBuffer);
postMethod.releaseConnection();
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
相关文章推荐
- 使用Httpclient向百度翻译提交POST请求 接受返回的JSON字符串 实现翻译功能(2)----Httpclient使用详解
- 使用Httpclient向百度翻译提交POST请求 接受返回的JSON字符串 实现翻译功能(3)----BufferedReader与BufferedWriter的使用
- 使用Httpclient向百度翻译提交POST请求 接受返回的JSON字符串 实现翻译功能(1)----程序实现原理
- PHP使用curl请求实现post方式上传图片文件功能示例
- location的hash部分和使用window.onhashchange实现ajax请求内容时使用浏览器后退和前进功能
- 微信小程序实现快递查询功能(界面传值、JSON数据请求和解析、radio-group的使用...)
- 使用AsyncHttpClient 实现get和post请求
- 使用python登录新浪API,实现自动转发功能
- 使用SecureCRT转发功能,实现外网访问内网服务
- 使用AsyncHttpClient 实现get和post请求
- Java使用HttpClient实现Post请求
- java使用过滤器实现 请求转发
- 通过Socket实现简单web服务器功能(接收页面请求信息)不使用Tomcat!
- 如何使用fiddler转发请求功能
- Java使用HttpClient实现Post请求
- Java使用HttpClient实现Post请求实例
- 几种方法实现ajax请求内容时使用浏览器后退和前进功能
- 使用HttpClient实现Post请求和Get请求
- 使用RxJava来实现网络请求轮询功能
- 使用HttpClient发送HttpPost请求包含上传本地图片和远程图片的传输实现