2012-11-27 72 views
2

我試圖連接一個http url.但它顯示「已連接」異常。 也請找到我的基本認證的任何問題。HttpURLConnection問題

HttpURLConnection conn = (HttpURLConnection) url 
       .openConnection(); 
    conn.setRequestMethod("POST"); 

    BASE64Encoder enc = new sun.misc.BASE64Encoder(); 
    String userpassword = user + ":" + password; 
    String encodedAuthorization = enc.encode(userpassword.getBytes()); 
    conn.setRequestProperty("Authorization", "Basic "+ encodedAuthorization); 
    conn.setDoInput(true); 
    conn.setDoOutput(true); 
    conn.setRequestMethod("POST"); 
    conn.setRequestProperty("Content-Length", 
    String.valueOf(data.length())); 
    conn.setRequestProperty("Content-Type","application/x-www- form-urlencoded"); 
    conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); 
+0

我的猜測是第二次調用conn.setRequestMethod(「POST」);刪除它並嘗試。 –

+0

thanks..bt no change –

+1

好的,發佈完整的代碼和異常堆棧跟蹤。事實上堆棧跟蹤應該包含確切的行號 –

回答

0

使用此

URLConnection conn = url.openConnection(); 
if (conn instanceof HttpsURLConnection) { 
((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory); 
((HttpsURLConnection) conn).setRequestMethod(POST); 
} 

您可以從here得到幫助。

+0

好..謝謝..我會嘗試 –

相關問題