2012-09-13 349 views
2

我檢查了這個Post。但我不明白什麼是邏輯錯誤。我仍然收到這個錯誤。我試圖轉儲模擬器流量。但是我仍然不明白問題的癥結所在。HTTPURLConnection - 400錯誤請求

從流量轉儲中,這是Android向服務器發送的請求。你也可以看到反應:

GET /Authenticate/ HTTP/1.1 
Authorization: Basic asdfasdfasdfas 

Accept-Charset: UTF-8 
Host: www.domain.com 
User-Agent: Dalvik/1.4.0 (Linux; U; Android 2.3.3; sdk Build/GRI34) 
Connection: Keep-Alive 
Accept-Encoding: gzip 

neQPˆ? 6 6 RT 4VRT 5 E (
» @ÍCl¦' 
PÙ[ ˜ároP"8" neQPI " " RT 4VRT 5 E 
¼ @ËVl¦' 
PÙ[ ˜ároP"8«‹ HTTP/1.1 400 Bad Request 
Date: Thu, 13 Sep 2012 04:47:42 GMT 
Server: Apache/2.2.15 (CentOS) 
Content-Length: 310 
Connection: close 
Content-Type: text/html; charset=iso-8859-1 

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 
<html><head> 
<title>400 Bad Request</title> 
</head><body> 
<h1>Bad Request</h1> 
<p>Your browser sent a request that this server could not understand.<br /> 
</p> 
<hr> 
<address>Apache/2.2.15 (CentOS) Server at www.domain.com Port 80</address> 
</body></html> 
neQPé¬ 6 6 RT 4VRT 5 E (
½ @ÍAl¦' 
PÙ[ ™îároP"8 , neQPéË @ @ RT 5RT 4V E (çØ@ @³- 
l¦'Ù[ Páro ™îP)E   neQPéË @ @ RT 5RT 4V E (çÙ@ @³, 
l¦'Ù[ Páro ™ïP)D   neQPö「 
    © © RT 5RT 4V E ›k‹@ @¶Á 

    ³ër,9Jr ‘Pÿÿ6B WRTE w [ * ¨­«º[ 09-13 04:47:41.822 446:0x1c7 D/400  ] 
    text/html; charset=iso-8859-1Bad Request 

我不知道那些額外的字符是什麼意思。但我試圖從中找出問題。

這是基本的代碼:

String credentials = username + ":" + password; 
byte[] toencode = null; 
try { 
    toencode = credentials.getBytes("UTF-8"); 
} catch (UnsupportedEncodingException e1) { 
    e1.printStackTrace(); 
} 
HttpURLConnection conn = null; 
try { 
    //Utilities.isNetworkAvailable(context); 
    URL url = new URL(params[0]); 
    Log.d(params[0],"UR"); 
    conn = (HttpURLConnection) url.openConnection(); 
    conn.setRequestProperty("Authorization", "Basic " + Base64.encodeToString(toencode, Base64.DEFAULT)); 
    conn.setRequestProperty("Accept-Charset", "UTF-8"); 
    conn.setRequestProperty("Host", "www.domain.com"); 
    conn.setRequestMethod("GET"); 
    conn.setDoOutput(true); 

    String data = conn.getInputStream().toString(); 
    return data; 
} 

任何想法?

更新

我檢查網絡服務器日誌,看是否請求被擊中服務器,如果有一個與請求的任何問題。這是我從錯誤日誌中看到的:

[Thu Sep 13 10:05:24 2012] [error] [client 91.222.195.132] client sent HTTP/1.1 request without hostname (see RFC2616 section 14.23): /Authenticate/ 
[Thu Sep 13 23:11:57 2012] [error] [client 91.222.195.132] client sent HTTP/1.1 request without hostname (see RFC2616 section 14.23): /Authenticate/ 
[Thu Sep 13 23:12:03 2012] [error] [client 91.222.195.132] client sent HTTP/1.1 request without hostname (see RFC2616 section 14.23): /Authenticate/ 

但是,我正在設置請求的標頭屬性。

任何想法?

+0

你的URL的價值是什麼? – Lucifer

+0

其http://www.domain.com/Authenticate/ –

回答

2

我想通了這個自己。它與設置標題的順序有關。

編輯:我使用的順序。

URL url = new URL(strUrl); 
conn = (HttpURLConnection) url.openConnection(); 
conn.setRequestMethod("GET"); 
conn.setRequestProperty("Host", "myhost.com"); 
conn.setRequestProperty("Authorization", "Basic " + Base64.encodeToString(toencode, Base64.DEFAULT)); 
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:1.3.1)"); 
conn.setRequestProperty("Accept-Charset", "UTF-8"); 

conn.setConnectTimeout (5000) ; 
conn.setDoOutput(true); 
conn.setDoInput(true); 
+2

正確的順序是? – wangqi060934

+0

更新了我的答案! –

+0

據我所知,setDoOutput覆蓋了他正在試圖用「POST」做出的「GET」......這樣說,我不知道它是否工作肯定... – jcaruso

0

試試這個

static final String _url = "http://www.google.com"; 
static final String charset = "UTF-8"; 

// to build the query string that will send the message 
private static String buildRequestString(String param1, 
     String param2, String param3, String param4, String param5) 
     throws UnsupportedEncodingException { 
    String[] params = new String[5]; //customize this as per your need 
    params[0] = param1; 
    params[1] = param2; 
    params[2] = param3; 
    params[3] = param4; 
    params[4] = param5; 

    String query = String.format(
      "uid=%s&pwd=%s&msg=%s&phone=%s&provider=%s", 
      URLEncoder.encode(params[0], charset), 
      URLEncoder.encode(params[1], charset), 
      URLEncoder.encode(params[2], charset), 
      URLEncoder.encode(params[3], charset), 
      URLEncoder.encode(params[4], charset)); 
    return query; 

} 

public static void doSomething(String param1, String param2, 
     String param3, String param4, String param5) throws Exception { 
    // To establish the connection and perform the post request 
    URLConnection connection = new URL(_url 
      + "?" 
      + buildRequestString(param1, param2, param3, param4, 
        param5)).openConnection(); 
    connection.setRequestProperty("Accept-Charset", charset); 
    // This automatically fires the request and we can use it to determine 
    // the response status 
    InputStream response = connection.getInputStream(); 
    BufferedReader br = new BufferedReader(new InputStreamReader(response)); 
    // This stores the response code. 
    // Any call to br.readLine() after this is null. 
    responsecode = br.readLine(); 
    // And this logs the already stored response code 
    Log.d("ServerResponse", responsecode); 
    responseInt = Integer.valueOf(responsecode).intValue(); 
} 
+0

我明白你想說什麼。但我的網址沒有任何查詢字符串。它只是直接http://www.domain.com/Authenticate/與正在形成正確的基本身份驗證標題。 –

+0

然後你可以使用這個查詢沒有任何參數。只要提及你的網址'static final String _url =「<你的URL HERE>」; ' –

+0

沒用。它已經是一個字符串。 –