当前位置: 代码迷 >> Java Web开发 >> java httpclient有关问题
  详细解决方案

java httpclient有关问题

热度:6253   发布时间:2016-04-10 23:01:41.0
java httpclient问题
我写了一个demo,功能是多线程httpclient并发访问某个网站,最后返回的结果有好多500,还有好多refuse,我不知道这个500或者refuse是我的程序问题,还是我访问的那个网站的问题?
代码如下:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

public class httpclient {

    public static String url = "";
    public static CookieStore cookieStore;
    public static DefaultHttpClient httpClient = null;

    public static DefaultHttpClient getHttpClient() {
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
        HttpProtocolParams.setUseExpectContinue(params, true);

        //设置连接超时时间 
        int REQUEST_TIMEOUT = 30 * 1000; //设置请求超时10秒钟 
        int SO_TIMEOUT = 60 * 1000;  //设置等待数据超时时间10秒钟 
        HttpConnectionParams.setConnectionTimeout(params, REQUEST_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, SO_TIMEOUT);
//        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, REQUEST_TIMEOUT);
//        params.setParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT);

        //设置访问协议 
        SchemeRegistry schreg = new SchemeRegistry();
        schreg.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        schreg.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

        PoolingClientConnectionManager pccm = new PoolingClientConnectionManager(schreg);
        pccm.setDefaultMaxPerRoute(200); //每个主机的最大并行链接数 
        pccm.setMaxTotal(200); //客户端总并行链接最大数    
        HttpHost localhost = new HttpHost(url, 80);
        pccm.setMaxPerRoute(new HttpRoute(localhost), 50);
        httpClient = new DefaultHttpClient(pccm, params);
        return httpClient;
    }

    public static String httpSend(String xml, String url, boolean login, Count c) {
        HttpPost httppost = null;
        try {
            if (cookieStore == null && login) {
                cookieStore = new BasicCookieStore();
            }
            HttpContext localContext = new BasicHttpContext();
            localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

            StringEntity myEntity = new StringEntity(xml, ContentType.create("text/xml", "UTF-8"));
            httppost = new HttpPost(url);
  相关解决方案