47 lines
1.5 KiB
Java
47 lines
1.5 KiB
Java
package com.base.util;
|
|
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
|
|
|
|
import net.sf.json.JSONArray;
|
|
import net.sf.json.JSONObject;
|
|
|
|
import org.apache.commons.httpclient.HttpClient;
|
|
import org.apache.commons.httpclient.methods.PostMethod;
|
|
import org.apache.commons.httpclient.methods.RequestEntity;
|
|
import org.apache.commons.httpclient.methods.StringRequestEntity;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
public class HttpClientUtil {
|
|
|
|
public static String getNewsData(String type,String parms) throws Exception{
|
|
String url = "";
|
|
if("bannerList".equals(type)){
|
|
url="http://47.93.227.86/app_article/art/bannerList";
|
|
}else if ("artList".equals(type)){
|
|
url="http://47.93.227.86/app_article/art/artList";
|
|
}
|
|
//创建连接对象
|
|
HttpClient httpClient = new HttpClient();
|
|
//创建请求
|
|
PostMethod method = new PostMethod(url);
|
|
RequestEntity entity=new StringRequestEntity(parms,"application/json","UTF-8");
|
|
//设置请求体信息
|
|
method.setRequestEntity(entity);
|
|
//设置请求头信息
|
|
//method.setRequestHeader("APPKEY", "C6C6E17AC153ED8DF8D61207");
|
|
httpClient.executeMethod(method);
|
|
StringBuffer sb = new StringBuffer();
|
|
//获取返回信息
|
|
InputStream in = method.getResponseBodyAsStream();
|
|
InputStreamReader isr = new InputStreamReader(in, "UTF-8");
|
|
char[] b = new char[4096];
|
|
for(int n; (n = isr.read(b)) != -1;) {
|
|
sb.append(new String(b, 0, n));
|
|
}
|
|
return sb.toString();
|
|
}
|
|
}
|