Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress("10.0.0.172",80));//联通CTWAP代理地址是10.0.0.172 URL url = new URL(urlStr); HttpURLConnection conn=(HttpURLConnection) url.openConnection(proxy);
if (conn == null){ throw new IOException("URLConnection instance is null"); }
手机端代码
public String getNativePhoneNumber(Context context) throws Exception {
// String NativePhoneNumber=null;
// NativePhoneNumber=telephonyManager.getLine1Number();
// return NativePhoneNumber;
String result = "";
// String urlStr = "http://tv.ruyi.com/portal";
String urlStr = "http://XX.XX.XX.XX:XXXX/live/index.jsp";
Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress("10.0.0.172",80));//联通CTWAP代理地址是10.0.0.172
URL url = new URL(urlStr);
HttpURLConnection conn=(HttpURLConnection) url.openConnection(proxy);
if (conn == null){
throw new IOException("URLConnection instance is null");
}
conn.setConnectTimeout(30000);//
conn.setDoOutput(true); // 发送POST请求必须设置允许输出,表示允许对外输出
conn.setUseCaches(false); // 不使用Cache
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
int responseCode = conn.getResponseCode();
Log.e("IndexActivity","responseCode is:"+responseCode);
if(responseCode == 200){
InputStream stream = conn.getInputStream();
result = inStream2String(stream);
String temvit = result;
Log.e("error","result" + result);
if (temvit.substring(0, 3).trim().equals("861")||temvit.substring(0, 3).trim()=="861") {//此处视情况而定
result = temvit.substring(2, 13);
}
if (temvit.substring(0, 1).trim().equals("1")||temvit.substring(0, 1).trim()=="1") {//此处也是,视情况修改
result = temvit;
}
Log.e("error","result" + result);
}else{
Log.e("error","false");
}
// HttpClient client=new DefaultHttpClient();
// HttpPost request;
//
// request = new HttpPost(new URI(urlStr));
// HttpResponse response = client.execute(request);
// //判断请求是否成功
// if(response.getStatusLine().getStatusCode()==200){
// HttpEntity entity=response.getEntity();
// if(entity!=null){
// result = EntityUtils.toString(entity); //返回获取的值
// Log.e("error","result" + result);
// } else{
// result = "没有获取到任何数据!"; //返回空
// Log.e("error","result" + result);
// }
// }
// Log.e("error","result" + result);
return result;
}网页代码Enumeration headers = request.getHeaderNames() ;
while(headers.hasMoreElements())
{
String head = (String)headers.nextElement();
out.println(head+":"+request.getHeader(head));
// String mobile = request.getParameter("HTTP_X_UP_CALLING_LINE_ID");
// out.println("HTTP_X_UP_CALLING_LINE_ID"+":"+mobile);
}
但是获取不到HTTP_X_UP_CALLING_LINE_ID对应的值啊,是null
都能获取到手机型号信息,搞不明白为什么获取不到手机号,网页是放在联通提供的服务器上面的,是不是要做什么特殊的处理啊,应该不是简单的放在他们服务器上面就ok了。不知道页面上是不是要写一些调用联通接口的代码神马的啊
charset:UTF-8
content-type:text/xml; charset=UTF-8
user-agent:Dalvik/1.4.0 (Linux; U; Android 2.3.4; GT-S5570 Build/GINGERBREAD)
host:10.82.52.16:8090
accept-encoding:gzip
cookie:
connection:close
HTTP_X_UP_CALLING_LINE_ID:null
这是访问页面输出的结果,手机的型号都能获取到,为什么手机号没呢
URL url = new URL(urlStr);
HttpURLConnection conn=(HttpURLConnection) url.openConnection(proxy);
这不是通过联通的wap接入的吗
"10.0.0.172",80这个是3G wap这个是联通3G卡连接上网的接入点的地址和端口,需要访问的页面只有手机端通过这个接入点连接上网才能访问到要访问的页面
大家帮我看看我这结果,是不是解析的有问题啊
accept:*/*
charset:UTF-8
content-type:text/xml; charset=UTF-8
user-agent:Dalvik/1.4.0 (Linux; U; Android 2.3.4; GT-S5570 Build/GINGERBREAD)
host:XX.XX.XX.XX:XXXX
accept-encoding:gzip
cookie:
connection:close
你手机设置wap访问试试。
String result = "";
String urlStr = "http://v.17186.cn/test.jsp";
Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress("10.0.0.172",80));//联通CTWAP代理地址是10.0.0.172
URL url = new URL(urlStr);
HttpURLConnection conn=(HttpURLConnection) url.openConnection(proxy);
if (conn == null){
throw new IOException("URLConnection instance is null");
}
conn.setConnectTimeout(30000);//
conn.setDoOutput(true); // 发送POST请求必须设置允许输出,表示允许对外输出
conn.setUseCaches(false); // 不使用Cache
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
int responseCode = conn.getResponseCode();
Log.e("IndexActivity","responseCode is:"+responseCode);
if(responseCode == 200){
InputStream stream = conn.getInputStream();
// stream = new GZIPInputStream(stream);
result = inStream2String(stream);
String temvit = result;
//Log.e("error","result" + result);
int x = temvit.indexOf("<body>");
temvit = temvit.substring(x);
result = temvit.substring(7,24).trim();
Log.e("error","result: " + result);
}
return result;
}
public String inStream2String (InputStream in) throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
return out.toString();
}