当前位置: 代码迷 >> Android >> 程序中透过wap联网方式访问网络
  详细解决方案

程序中透过wap联网方式访问网络

热度:61   发布时间:2016-04-28 08:15:21.0
程序中通过wap联网方式访问网络
用户的联网方式大致有 wap net wifi 三种. 使用android包中提供的函数,HttpConnection只能使用net和wifi,wap方式无法访问. 

问? 如何让程序可以通过wap访问网络?

------解决方案--------------------
if(bUsingProxy){
conn.setRequestProperty("X-Online-Host", host);
}
conn.setRequestProperty("Accept", "text/plain");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Connection", "Close");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
conn.connect();

------解决方案--------------------
给个完整的
private void doPost() throws IOException{

StringBuilder builder = new StringBuilder();
builder.append("http://");

if(bUsingProxy){
builder.append(proxy);
}else{
builder.append(host);
}

if(urlpage != null){
builder.append("/" + urlpage);
}

String desturl = builder.toString();
InputStream stream = null; 
HttpURLConnection conn = null;
try{
byte[] data = params.getBytes();

URL url = new URL(desturl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");

conn.setConnectTimeout(connectTimeout);
conn.setReadTimeout(readTimeout);
conn.setDoOutput(true);
conn.setUseCaches(false);
if(bUsingProxy){
conn.setRequestProperty("X-Online-Host", host);
}
conn.setRequestProperty("Accept", "text/plain");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Connection", "Close");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
conn.connect();

OutputStream outputStream = conn.getOutputStream();
outputStream.write(data);
outputStream.flush();
outputStream.close();

int resp_code = conn.getResponseCode();
Map<String, List<String>> headerMap = conn.getHeaderFields();
  相关解决方案