使用3g的http帖子失败了

时间:2018-05-08 06:30:49

标签: java android wcf post http-post

在这种方法中,我正在尝试连接到WCF,但是当我使用3g时,它无法正常工作。当我使用WIFI时,它的工作正常。

public static String postUsingWS(String link,
        HashMap<String, String> key_values, Integer timeout)
        throws Exception {

    HttpClient client = getNewHttpClient(timeout);
    HttpPost post = new HttpPost(link);
    pairs = new ArrayList<NameValuePair>();
    if (key_values != null) {
        Set<String> keys = key_values.keySet();
        for (String key : keys) {
            pairs.add(new BasicNameValuePair(key, key_values.get(key)));

        }
    }

    post.setEntity(new UrlEncodedFormEntity(pairs));
    HttpResponse httpresponse = client.execute(post);
    stringresponse = EntityUtils
            .toString(httpresponse.getEntity(), "UTF-8");
    return stringresponse;
}

此方法可以使用https和http。

进行连接
private static HttpClient getNewHttpClient(Integer timeout) { 
       try { 
           KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
           trustStore.load(null, null); 

           SSLSocketFactory sf = new SSLSocketFactory(trustStore); 
           sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 

           HttpParams params = new BasicHttpParams(); 
           HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
           HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
           HttpProtocolParams.setUseExpectContinue(params, false);
           HttpConnectionParams.setConnectionTimeout(params,
                    timeout == null ? getTimeout() : timeout);
            HttpConnectionParams.setSoTimeout(params,
                    timeout == null ? getTimeout() : timeout);
        params.setParameter("Connection", "keep-alive");

           SchemeRegistry registry = new SchemeRegistry(); 
           registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
           registry.register(new Scheme("https", sf, 443)); 
           SSLContext ctx = SSLContext.getInstance("TLS");
           ctx.init(null, new TrustManager[] {
             new X509TrustManager() {
               public void checkClientTrusted(X509Certificate[] chain,String authType) {}
               public void checkServerTrusted(X509Certificate[] chain, String authType) {}
               public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; }
             }
           }, null);
           HttpsURLConnection.setDefaultSSLSocketFactory(new MyTLSSocketFactory());

           return new DefaultHttpClient(params); 
       } catch (Exception e) { 
           return new DefaultHttpClient(); 
       } 
    }

另外从浏览器我无法通过3g打开网络服务,只有wifi

0 个答案:

没有答案