我正在尝试连接一个 http url。但它显示“已连接”异常。还请发现我的基本身份验证有任何问题。
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)");