用户的联网方式大致有 wap net wifi 三种. 使用android包中提供的函数,HttpConnection只能使用net和wifi,wap方式无法访问. 问? 如何让程序可以通过wap访问网络?

解决方案 »

  1.   

    wap上网是要经过代理的,取得代理地址 设置代理
      

  2.   

    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();
      

  3.   

    给个完整的
    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();
    listener.action(NetworkListener.RESPONSE_CODE, new Integer(resp_code));

    if(resp_code == HttpURLConnection.HTTP_OK){
    stream = conn.getInputStream();
    if(stream != null){
    Log.d("cn.cmcc.t", "GETRESPONSE:" + resp_code);
    }
    if(mCommand != -1)
    listener.action(NetworkListener.RECEIVE_STREAM, new Object[]{new Integer(mCommand),stream});
    else
    listener.action(NetworkListener.RECEIVE_STREAM, stream);
    }

    } catch (IOException e) {
    e.printStackTrace();
    listener.action(NetworkListener.REQUEST_TIMEOUT, null);
    } finally{
    if(stream != null)
    stream.close();
    if(conn != null)
    conn.disconnect();
    }
    }
      

  4.   

    楼上那样做只能支持移动的cmwap吧 “X-Online-Host”只是移动私有的扩展头吧
      

  5.   

    感谢上面两位, 以前搜这个问题的资料都不知道怎么搜. 因为搜android cmwap 全部是android操作的答案. 现在知道用代理了. 就大体明白怎么做了.多谢两位. 
      

  6.   

    楼主能告诉我,你说的三种方法中,net是如何上网的
      

  7.   

    恩,感谢楼上两位,我已经实现了.  另外我还发现了一个要点:就是不管原来网页是何编码方式,用了这个cmwap后都变成了utf8另外,一点,联通的cmwap网关也是10.00.00.172 么? 如何获取cmwap的网关ip和端口呢? 两位回来拿分的时候,帮忙解答一下. 
      

  8.   


    额? net方式, 不用做其他设置,就照普通的来,就可以上网的.... 我也很奇怪.
      

  9.   

    android.net.Proxy.getHost(context);获取网关IP
    android.net.Proxy.getPort(context);获得端口
      

  10.   

    在setRequestProperty的时候,可以设置"Accept-Charset"来改变编码格式,移动是10.0.0.172,电信是10.0.0.200,端口都是80,联通的不知道