由于移动端要显示业务系统的数据,但是业务系统众多,如果直接和业务系统交互,会紧耦合,杂乱无章,不好管理,特编写请求转发组件,实现统一中转。
本文基于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 Str