<span style="white-space:pre"> </span><h2 class="entry_title">java利用https请求服务器</h2> <span style="font-size:18px;">sun.net.www.protocol.http.HttpURLConnection cannot be </span>
<span style="font-size:18px;"><span style="white-space: pre;"><span style="white-space:pre"> </span>cast to </span><span style="font-family: Arial, Helvetica, sans-serif;">javax.net.ssl.HttpsURLConnection</span></span>
</pre><pre name="code" class="java">
package com.service;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import sun.net.www.protocol.https.Handler;
import com.weixin.util.MyX509TrustManager;
/**
* 地理位置处理类
*
* @author yiyunqi
* @date 2015-11-17
*/
public class LocaltionService {
private static String LOCALTION_URL = "http://api.map.baidu.com/geocoder/v2/?location=22.685970,114.069473&output=xml&ak=<61f8bd72d68aefxxxxxxxxx&pois=1&Precision=65";
public static void main(String[] args) {
getXml("","");
}
public static String getXml(String lat,String lng){
StringBuffer buffer = new StringBuffer();
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(LOCALTION_URL);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
httpUrlConn.setSSLSocketFactory(ssf);
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// 设置请求方式(GET/POST)
httpUrlConn.setRequestMethod("POST");
// 当有数据需要提交时
if (null != "{\"Precision\":\"65.000000\"}") {
OutputStream outputStream = httpUrlConn.getOutputStream();
// 注意编码格式,防止中文乱码
outputStream.write("{\"Precision\":\"65.000000\"}"
.getBytes("UTF-8"));
outputStream.close();
}
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
} catch (Exception e) {
e.printStackTrace();
}
return buffer.toString();
}
}
</pre><pre name="code" class="java"><span style="white-space:pre"> java.lang.ClassCastException: sun.net.www.protocol.http.HttpURLConnection cannot be cast to javax.net.ssl.HttpsURLConnection
<span style="white-space:pre"> </span>at com.service.LocaltionService.getXml(LocaltionService.java:43)
<span style="white-space:pre"> </span>at com.service.LocaltionService.main(LocaltionService.java:30)</span>
关于:sun.net.www.protocol.http.HttpURLConnection cannot be cast to javax.net.ssl.HttpsURLConnection 异常的处理
原因:
在没有对使用的SSL实现类进行配置的情况下,在程序中如果正常使用java.net.URL的不带 URLStreamHandler 参数的构造方法new 一个URL对象的话,url.openConnection()默认是返回sun.net.www.protocol.http.HttpURLConnection 类型对象。所以我们带上一个URLStreamHandler
参数。
URL url = new URL(null,url,new Handler());