package com.heaven.shopping.net;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.heaven.shopping.ConstantValue;
import com.heaven.shopping.GloableParams;
public class HttpClientAdapter {
private HttpClient client;
private HttpRequest request;
private HttpGet get;
private HttpPost post;
private HttpResponse response;
public static BasicHeader[] headers = new BasicHeader[10];
static {
headers[0] = new BasicHeader("Appkey", "12343");
headers[1] = new BasicHeader("Udid", "");// 手机串号
headers[2] = new BasicHeader("Os", "android");//
headers[3] = new BasicHeader("Osversion", "");//
headers[4] = new BasicHeader("Appversion", "");// 1.0
headers[5] = new BasicHeader("Sourceid", "");//
headers[6] = new BasicHeader("Ver", "");
headers[7] = new BasicHeader("Userid", "");
headers[8] = new BasicHeader("Usersession", "");
headers[9] = new BasicHeader("Unique", "");
}
public HttpClientAdapter()
{
client=new DefaultHttpClient();
if(StringUtils.isNotBlank(GloableParams.PROXY_IP))
{
//wap方式
HttpHost host=new HttpHost(GloableParams.PROXY_IP, GloableParams.PROXY_PORT);
client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, host);
}
}
public String sendPost(String url,Map<String,String> params)
{
post=new HttpPost(url);
post.setHeaders(headers);
try {
//设置参数信息
if(params!=null)
{
List<BasicNameValuePair> pairs=new ArrayList<BasicNameValuePair>();
for(Map.Entry<String, String> item:params.entrySet())
{
BasicNameValuePair pair=new BasicNameValuePair(item.getKey(), item.getValue());
pairs.add(pair);
}
HttpEntity entity=new UrlEncodedFormEntity(pairs, ConstantValue.CHARSET);
post.setEntity(entity);
}
response = client.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
return EntityUtils.toString(response.getEntity(), ConstantValue.CHARSET);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
}