• HttpClientAdapter(工具类)


    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 "";
    }

    }

  • 相关阅读:
    c#发送邮件.net1.1和.net2.0中的两个方法
    六步使用ICallbackEventHandler实现无刷新回调
    报表项目总结
    转载:Tomcat Port 8009 与AJP13协议
    JUnit4 使用指南二 (熟练掌握)
    JUnit4 使用指南一 (简单上手)
    HP的项目中曾做一个业务日志系统
    Unitils使用(转载)
    iBatis 学习
    JUnit4 使用指南三 (Runner 特性分析)
  • 原文地址:https://www.cnblogs.com/gsdimz/p/3088249.html
Copyright © 2020-2023  润新知