2010-09-27 44 views
4

我是一個HttpsURLConnection的問題,我似乎無法解決。基本上,我向服務器發送了一些信息,如果某些數據錯誤,服務器會向我發送一個500響應代碼。但是,它也會在響應中發送一條消息,告訴我哪一位數據是錯誤的。問題在於,當我讀入消息時,消息總是空的。我認爲這是因爲在讀取流之前總是會拋出一個filenotfound異常。我對嗎?我也嘗試閱讀errorstream,但這總是空的。這裏有一個片段:HttpsURLConnection連接問題

conn = (HttpsURLConnection) connectURL.openConnection(); 
    conn.setDoOutput(true); 
    conn.setConnectTimeout(30000); 
    conn.setReadTimeout(30000); 
    conn.setRequestMethod("POST"); 
    conn.setRequestProperty("Content-Length", 
     Integer.toString(outString.getBytes().length)); 
    DataOutputStream wr = new DataOutputStream(conn 
     .getOutputStream()); 
    wr.write(outString.getBytes()); 
    wr.flush(); 
    wr.close(); 
    if(conn.getResponseCode>400{ 

    String response = getErrorResponse(conn); 

    public String getErrorResponse(HttpsURLConnection conn) { 
    Log.i(TAG, "in getResponse"); 
    InputStream is = null; 
    try { 

    //is = conn.getInputStream(); 
    is = conn.getErrorStream(); 
    // scoop up the reply from the server 
    int ch; 
    StringBuffer sb = new StringBuffer(); 
    while ((ch = is.read()) != -1) { 
    sb.append((char) ch); 
    } 
    //System.out.println(sb.toString()); 
    return sb.toString(); 
    // return conferenceId; 
    } 
    catch (Exception e){ 
    e.printStackTrace(); 
    } 
    } 

回答

2

所以纔跟進這一點,這裏是我如何解決它:

public static String getResponse(HttpsURLConnection conn) { 
    Log.i(TAG, "in getResponse"); 
    InputStream is = null; 
    try { 
     if(conn.getResponseCode()>=400){ 
      is = conn.getErrorStream(); 
     } 
     else{ 
      is=conn.getInputStream(); 
     } 
     ...read stream... 
} 

看來,稱他們是這樣產生的一個消息的錯誤流。感謝您的建議!

0

嘗試設置內容類型的請求屬性"application/x-www-form-urlencoded"

同此鏈接中提到: http://developers.sun.com/mobility/midp/ttips/HTTPPost/

The Content-Length and Content-Type headers are critical because they tell the web server how many bytes of data to expect, and what kind, identified by a MIME type.

In MIDP clients the two most popular MIME types are application/octet-stream, to send raw binary data, and application/x-www-form-urlencoded, to send name-value pairs

0

你在控制服務器?換句話說,您是否編寫了在服務器上運行的進程,並偵聽您嘗試訪問的端口?

如果你沒有,那麼你也應該能夠調試一下,看看爲什麼你的過程返回404

如果沒有,則說明您的體系結構(HTTP服務器,該組件將調用來回應你的HTTP(S)請求等),我們會從那裏拿走它。

在最簡單的情況下,HTTP服務器是一個Apache服務器,可以控制某些PHP腳本,這意味着Apache無法將您的請求分配給任何東西。很可能是Web服務器配置錯誤。提供更多的細節,我們會幫助你。