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)");
我的猜測是第二次調用conn.setRequestMethod(「POST」);刪除它並嘗試。 –
thanks..bt no change –
好的,發佈完整的代碼和異常堆棧跟蹤。事實上堆棧跟蹤應該包含確切的行號 –