2013-03-12 173 views
1

我試圖通過URL登錄和我收到的狀態碼500 httpurlconnevtion問題HttpURLConnection的獲取狀態500

public static String excutePost(String targetURL, String urlParameters) 
    { 
    URL url; 
    HttpURLConnection connection = null; 
    try { 
     //Create connection 
     url = new URL(targetURL); 
     connection = (HttpURLConnection)url.openConnection(); 
     connection.setRequestMethod("POST"); 
     connection.setRequestProperty("Content-Type", 
      "application/x-www-form-urlencoded"); 
     connection.setRequestProperty("Connection", "Keep-Alive");  
     connection.setRequestProperty("Content-Length", "" + 
       Integer.toString(urlParameters.getBytes().length)); 
     connection.setRequestProperty("Content-Language", "en-US"); 
     connection.setRequestProperty("Accept-Encoding", ""); 
     connection.setUseCaches (false); 
     connection.setDoInput(true); 
     connection.setDoOutput(true); 

     //Send request 
     DataOutputStream wr = new DataOutputStream (
        connection.getOutputStream()); 
     wr.writeBytes (urlParameters); 
     wr.flush(); 
     wr.close(); 

     System.out.println("status :"+connection.getResponseCode()); 
     System.out.println("getErrorStream() :"+connection.getErrorStream()); 
     //Get Response  
     InputStream is = connection.getInputStream(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
     String line; 
     StringBuffer response = new StringBuffer(); 
     while((line = rd.readLine()) != null) { 
     response.append(line); 
     response.append('\r'); 
     } 
     rd.close(); 
     return response.toString(); 

    } catch (Exception e) { 

     e.printStackTrace(); 
     return null; 

    } finally { 

     if(connection != null) { 
     connection.disconnect(); 
     } 
    } 
    } 

和我的PARAMS是

String urlParameters = 
         "pwd1=" + URLEncoder.encode("DEMO123", "UTF-8") + 
         "&badge=" + URLEncoder.encode("1233", "UTF-8"); 

我得到的logcat

status :500 
getErrorStream() :[email protected] 

謝謝

**EDITED 2** 

我也有

DataOutputStream dos = new DataOutputStream(connection.getOutputStream()); 

     // Add badge 
     dos.writeBytes(LINE_START + BOUNDRY + LINE_END); 
     dos.writeBytes("Content-Disposition: form-data; name='badge';"); 
     dos.writeBytes(LINE_END + LINE_END); 
     dos.writeBytes("1233"); 
     dos.writeBytes(LINE_END); 

     // Add password 
     dos.writeBytes(LINE_START + BOUNDRY + LINE_END); 
     dos.writeBytes("Content-Disposition: form-data; name='pwd1';"); 
     dos.writeBytes(LINE_END + LINE_END); 
     dos.writeBytes("DEMO123"); 
     dos.writeBytes(LINE_END); 

回答

0

狀態代碼嘗試500意味着Internal Server Error。爲什麼這是拋給你的,只有targetURL背後的服務器知道。

驗證您是否正確使用API​​。看一下響應的主體(除了狀態碼)可能會提供一個提示。

+0

如何檢查身體? – Android 2013-03-12 11:56:05

+0

@Android check connection.getErrorStream(); – 2014-10-14 20:44:59

1

500表示Internal Server Error。在你身邊可能沒有錯誤,它在服務器上。即使你發送的東西不正確,導致服務器返回500,仍然是服務器問題。

編輯: 歐凱,服務器而應返回類似400 Bad Request代替500 Internal Server Error但我現在發現你的錯誤:

​​

這裏的問題是,你首先從urlParameters的字節使用getBytes其中(引用javadoc):

使用平臺的默認字符集將此字符串編碼爲一個字節序列

然後你寫使用DataOutputStream.writeBytes它(引用的javadoc)字節:

字符串中的每個字符寫出來,依次通過丟棄其高八位。

總之,您的Content-Length與數據不符。因此,服務器將返回的

產生java.io.IOException:20個字節

超過內容長度限制

解決方案:

//consider urlParameters.getBytes("UTF-8") method instead of using default encoding 
byte[] bodyData = urlParameters.getBytes(); 
connection.setRequestProperty("Content-Length", Integer.toString(bodyData.length)); 

... 

//Send request 
InputStream out = connection.getOutputStream(); 
out.write(bodyData); 

編輯2: 編輯1絕對有效,但是,再次查看問題,我相信錯誤肯定是由服務器引起的。我認爲服務器返回了一個錯誤的Content-Length標題,並且在Android上讀取數據時,系統意識到服務器發送的數據多於應該由Content-Length發送的數據,並引發異常,同時也將狀態代碼替換爲500因爲它確實是一個服務器錯誤。

編輯3:

connection.setRequestProperty("Content-Language", "en-US"); 
connection.setRequestProperty("Accept-Encoding", ""); 

而不是設置Content-Language這這裏時並不需要的,你應該設置Content-EncodingUTF-8和,而不是空Accept-Encoding,你應該添加真正的預期MIME類型。我相信這是一個服務器錯誤,但是如果您正確設置了請求標頭,也許它不會出現。

+0

謝謝,java.io.IOException:超過20個字節的內容長度限制是否有任何問題 – Android 2013-03-12 11:11:25

+0

@test是的,這是一個服務器問題。向我們顯示服務器代碼 – Sulthan 2013-03-12 11:23:19

+0

它與ios完美合作,我在我的問題中添加了服務器代碼plz檢查 – Android 2013-03-12 11:54:56

相關問題