2

我正在尝试连接一个 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)");
4

1 回答 1

0

Use this

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

You can get help from here.

于 2012-11-27T05:35:41.450 回答