hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rusty <dongliang0...@gmail.com>
Subject HttpGet Method failed: HTTP/1.0 503 Service Unavailable-Help!
Date Wed, 29 Feb 2012 13:13:00 GMT
Hello all guys,thanks for reading
Such an annoying problem occurred to me,hoping someone help me
The problem:

1.threads share a  DefaultHttpClient
1)Defination
private static final ThreadSafeClientConnManager cm = new
ThreadSafeClientConnManager();
private static HttpHost proxy = new HttpHost("127.0.0.1",8086,"http");
private static DefaultHttpClient http = new DefaultHttpClient(cm);

2)and in my inital function

cm.setMaxTotal(100);
http.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

3)and then is my thread function
public static String getUrl(String url, String Chareset)
    {
      HttpGet get = new HttpGet(url);//uri
      get.setHeader("Content-Type", "text/html");
      get.setHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0;
Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50215;)");
      get.setHeader("Accept-Charset",
Chareset+";q=0.7,*;q=0.7");//"utf-8;q=0.7,*;q=0.7");
get.getParams().setParameter("http.socket.timeout",new
Integer(CONNECTION_TIMEOUT));//20000

      String result = "";
      try {
      HttpResponse response = http.execute(get);
if (response.getStatusLine().getStatusCode() != 200){//statusCode !=
HttpStatus.SC_OK) {
System.err.println("HttpGet Method failed: "
+ response.getStatusLine());//httpGet.getStatusLine()
}
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
EntityUtils.consume(entity);
entity = null;
}
} catch(java.net.SocketException ee)
{
ee.printStackTrace();
Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ee);
}
      catch (IOException e) {
//throw new Exception(e);
      Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null,
e);//TODO Debug
} finally {
get.abort();//releaseConnection();//TODO
http.getConnectionManager().shutdown();?
get = null;
}
      return result;
    }

4)And then I create 10 threads to call the getUrl function,but after about
1000 loops,shit happens:
*HttpGet Method failed: HTTP/1.0 503 Service Unavailable*
But I used IE and the proxy to open the url ,it's opened successfully.So it
seems nothing wrong with my proxy.
So what's wrong?


2.Then I changed the creation of httpclient to the getUrl() function,so
threads don't share  DefaultHttpClient,like that:

public static String getUrl(String url, String Chareset)
    {
      HttpGet get = new HttpGet(url);//uri
      get.setHeader("Content-Type", "text/html");
      get.setHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0;
Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50215;)");
      get.setHeader("Accept-Charset",
Chareset+";q=0.7,*;q=0.7");//"utf-8;q=0.7,*;q=0.7");
get.getParams().setParameter("http.socket.timeout",new
Integer(CONNECTION_TIMEOUT));//20000

        *DefaultHttpClient http = new DefaultHttpClient(cm); *
*        http.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
proxy);*

      String result = "";
      try {
      HttpResponse response = http.execute(get);
if (response.getStatusLine().getStatusCode() != 200){//statusCode !=
HttpStatus.SC_OK) {
System.err.println("HttpGet Method failed: "
+ response.getStatusLine());//httpGet.getStatusLine()
}
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
EntityUtils.consume(entity);
entity = null;
}
} catch(java.net.SocketException ee)
{
ee.printStackTrace();
Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ee);
}
      catch (IOException e) {
//throw new Exception(e);
      Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null,
e);//TODO Debug
} finally {
get.abort();//releaseConnection();//TODO
http.getConnectionManager().shutdown();?
get = null;
                *http = null;*
}
      return result;
    }

and then after about 600 loops of 10 threads,another shit happens:
*Exception in thread "Thread-11" java.lang.OutOfMemoryError: Java heap space
*
*Exception occurs in *result = EntityUtils.toString(entity);

So,really need some help.
Thanks!

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message