2012-08-09 59 views
0

我使用的是Apache HTTPClient 4.2。我使用下面的代碼來獲取HTTP狀態碼。它給我404HTTPClient給404沒有WWW的URL url

DefaultHttpClient client = new DefaultHttpClient(); 
     HttpContext context = new BasicHttpContext(); 
     HttpUriRequest method = new HttpGet("http://michaelkimsal.com/blog/hosted-solr-or-lucene-service/comment-page-1/#comment-604253"); 
     try { 
      HttpResponse httpResponse = client.execute(method,context); 
      int status = httpResponse.getStatusLine().getStatusCode(); 
      System.out.println("The status is:"+status); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

但是,如果更改URL,以這個(我已添加到WWW網址),http://www.michaelkimsal.com/blog/hosted-solr-or-lucene-service/comment-page-1/#comment-604253它給了我200。什麼可能是這種行爲的原因?請有人幫助解決這個問題。

回答

1

http://michaelkimsal.com在你的情況下無效的域,瀏覽器在某些情況下是智能的,自動處理這種情況下,你應該使用www的適當域。

編輯 請serverault的this鏈路和wiki

它適用於谷歌,因爲它做了DNS解析設置爲。您也可以設置別名

+0

謝謝。但它適用於google.com和其他許多人。你說它不是一個有效的域名。我是新來的,我真的很想知道它不是一個有效的域名。 – Vinay 2012-08-09 06:07:09

+0

我已經編輯過mu答案,您還可以在wiki上查看更多 – 2012-08-09 06:16:22