首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

java httpclient有关问题

2014-01-21 
java httpclient问题我写了一个demo,功能是多线程httpclient并发访问某个网站,最后返回的结果有好多500,还

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);


            httppost.setEntity(myEntity);
            HttpResponse hr = httpClient.execute(httppost, localContext);
            String result = null;
            int code = hr.getStatusLine().getStatusCode();
            if (code == 200) {
                result = EntityUtils.toString(hr.getEntity());
                c.addCount();
            } else {
                System.out.println("code:" + code);
                c.addfailcount();
                return null;
            }
            return result;
        } catch (Exception e) {
            System.out.println(e.toString());
            c.addExCount();
            return null;
        } finally {
            httppost.releaseConnection();
        }
    }

    public static void morethread() {
        final String xml = "";
        int size = 100;
        final Count c = new Count();
        ExecutorService exec = Executors.newCachedThreadPool();
        final Semaphore semp = new Semaphore(size);
        for (int index = 0; index < size; index++) {
            Runnable run = new Runnable() {
                @Override
                public void run() {
                    try {
                        semp.acquire();
                        for (int i = 0; i < 20; i++) {
                            httpSend(xml, url, false, c);
                        }
                        semp.release();
                    } catch (Exception e) {
                        System.out.println(e);
                    }
                }
            };
            exec.execute(run);
        }
        exec.shutdown();
        while (!exec.isTerminated()) {
            try {
                exec.awaitTermination(500, TimeUnit.MILLISECONDS);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        System.out.println(c.getCount() + "\t" + c.getFailcount() + "\t" + c.getExCount());


    }

    public static void main(String[] args) {
        getHttpClient();
        morethread();
        httpClient.getConnectionManager().shutdown();
    }
}



这里ip没填,希望各位指点一下。
运行结果如下:
code:500
code:500
code:500
code:500
code:500
code:500
code:500
code:500
code:500
code:500
code:500
code:500
org.apache.http.conn.HttpHostConnectException
这里问题深入一点就是,我如何知道这个是不是我本机没有足够资源导致http连接建立不了?
[解决办法]
500是对方服务器错误,refuse应该是对方的安全措施不让你访问
[解决办法]
应该是sessionID的问题吧

热点排行