android 网络编程,客户端访问 https 域名服务器的问题

码仔2020 2012-09-10 11:39:06
最近遇到一个头疼的问题,android 网络编程,使用android 模拟器 https协议域名方式访问服务器 ,总是报 UnknownHostNameException ,如果用ip方式替换域名就没有返回值,也没报错。求各位大神指教啊。贴上我自己的方法和访问路径 大家帮帮忙。


public static String GetData(String URL) throws ClientProtocolException,
IOException {

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork()
.penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects().detectLeakedSqlLiteObjects()
.penaltyLog().penaltyDeath().build());

String returns = "";

HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory
.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
registry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));

DefaultHttpClient client = new DefaultHttpClient();
SingleClientConnManager mgr = new SingleClientConnManager(
client.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr,
client.getParams());
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
HttpPost httpPost = new HttpPost(URL);

HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
// 解析返回的内容
returns = EntityUtils.toString(response.getEntity());
}

return returns;
}

https://ibsbjstar.ccb.com.cn/app/B2CMainPlatV5?CCB_IBSVersion=V5&SERVLET_NAME=B2CMainPlatV5&TXCODE=100119

只要手机模拟器返回跟浏览器返回数据相同就ok了
...全文
243 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
码仔2020 2013-07-31
  • 打赏
  • 举报
回复
有可能是网络没连接上,这么久,我也忘记这了.....
PlayAround 2013-07-29
  • 打赏
  • 举报
回复
遇到一样的问题,求解答,

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧