Android 设置wap请求方式

公司有个需求,需要在程序启动的时候去请求电信的一个wap网关,获取用户的手机号码,因为之前没有涉及到这块,所以自己就使用了之前普通的http请求方式,结果返回的信息中手机号码字段死活都是null,换了httpClient等各种方式都不可以,后来在网上搜索资料的时候才知道,原来使用普通的http直连的方式是获取不到用户手机号码的,需要设置电信的CTWAP代理才可以,大致代码如下:


[java]  view plain  copy
  1. /* 
  2.  * 发送获取手机号码的请求 
  3.  * */  
  4. public static String sendPhoneNumRequest(String imsi,Context context)throws Exception {  
  5.     String result = "";  
  6.     String urlStr = "http://xxxxxxxxxxxxxxxx";  
  7.       
  8.     Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress("10.0.0.200",80));//电信CTWAP代理地址是10.0.0.200  
  9.     URL url = new URL(urlStr);  
  10.     HttpURLConnection conn=(HttpURLConnection) url.openConnection(proxy);  
  11.   
  12.     if (conn == null){  
  13.         throw new IOException("URLConnection instance is null");  
  14.     }  
  15.   
  16.     conn.setConnectTimeout(30000);//  
  17.     conn.setDoOutput(true); // 发送POST请求必须设置允许输出,表示允许对外输出  
  18.     conn.setUseCaches(false); // 不使用Cache  
  19.     conn.setRequestMethod("GET");  
  20.   
  21.     conn.setRequestProperty("Accept""*/*");  
  22.     conn.setRequestProperty("Connection""Keep-Alive");// 维持长连接  
  23.     conn.setRequestProperty("Charset""UTF-8");  
  24.     conn.setRequestProperty("Content-Type""text/xml; charset=UTF-8");  
  25.       
  26.     int responseCode = conn.getResponseCode();  
  27.     Log.i("IndexActivity","responseCode is:"+responseCode);  
  28.     if(responseCode == 200){  
  29.         InputStream stream = conn.getInputStream();  
  30.         result = inStream2String(stream);  
  31.     }else{  
  32.           
  33.     }  
  34.       
  35.     return result;  
  36. }   

核心代码:
  1.     String urlStr = "http://xxxxxxxxxxxxxxxx";  
  2.       
  3.     Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress("10.0.0.200",80));//电信CTWAP代理地址是10.0.0.200  
  4.     URL url = new URL(urlStr);  
  5.     HttpURLConnection conn=(HttpURLConnection) url.openConnection(proxy);  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值