HttpURLConnection问题

时间:2012-11-27 05:00:03

标签: java

我正在尝试连接一个http url.But它显示一个“已连接”的异常。 也请发现我的基本身份验证有任何问题。

  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)");

1 个答案:

答案 0 :(得分:0)

使用此

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

您可以从here获得帮助。