【问题标题】:HttpClient connecting to Https serverHttpClient 连接到 Https 服务器
【发布时间】:2012-09-21 16:47:20
【问题描述】:

通过跳过 SSL 证书连接到 https 服务器时(我的意思是允许所有主机)。 登录到这样的 https 服务器后,我们是否需要每次都登录才能触发 get 或 post 请求。

我正在android中尝试这个。 任何好的指针都会有所帮助。

使用 httpclient 登录 https 服务器(允许全部跳过 SSL) 登录后触发简单的 Get 请求。

这个简单场景有没有示例代码库。

【问题讨论】:

    标签: android https


    【解决方案1】:

    首先,您必须将代码放在异步任务中,因为网络调用不在主线程上运行。

    然后你可以像这样使用它:

    private RegistrationInfo AsyncRegisterDevice(
                    AndroidDeviceInfo deviceInfo, NetworkIdentification networkId, long NMEC) {
                RegistrationInfo reqResp = new Objects().new RegistrationInfo();
    
                try {
    
                    JSONStringer deviceRegistration = new JSONStringer().object()
                            .key("DeviceInfo").object().key("androidId")
                            .value(deviceInfo.androidId).key("imei")
                            .value(deviceInfo.imei).key("mac")
                            .value(deviceInfo.mac).key("brand")
                            .value(deviceInfo.brand).key("product")
                            .value(deviceInfo.product).key("model")
                            .value(deviceInfo.model).key("manufacturer")
                            .value(deviceInfo.manufacturer).key("device")
                            .value(deviceInfo.device).key("serial")
                            .value(deviceInfo.serial).key("carrierNumber")
                            .value(deviceInfo.carrierNumber).endObject()
                            .key("UserIdentification").object().key("userName")
                            .value(networkId.username).key("password")
                            .value(networkId.password).endObject()
                            .key("nmec").value(NMEC).endObject();
    
                    HttpPost request = new HttpPost(hostProtocol + "://"
                            + hostAddress + "/Services/Register.svc/Register");
                    request.setHeader("Accept", "application/json");
                    request.setHeader("Content-Type", "application/json");
    
                    StringEntity requestEntity = new StringEntity(
                            deviceRegistration.toString());
    
                    request.setEntity(requestEntity);
    
                    DefaultHttpClient httpClient = (DefaultHttpClient) CSRHttpClient
                            .getNewHttpClient();
    
                    String message = new String();
                    HttpEntity responseEntity = null;
    
                    try {
                        HttpResponse httpResponse = httpClient.execute(request);
                        responseEntity = httpResponse.getEntity();
                    } catch (Exception ex) {
                        message = ex.getMessage();
                        android.util.Log.e("CSR", message);
                        return new Objects().new RegistrationInfo();
                    }
    
                    if (responseEntity == null)
                        return reqResp;
    
                    char[] buffer = new char[(int) responseEntity
                            .getContentLength()];
                    InputStream stream = responseEntity.getContent();
                    InputStreamReader reader = new InputStreamReader(stream);
                    reader.read(buffer);
                    stream.close();
    
                    JSONObject jsonRegInfo = new JSONObject(new String(buffer));
    
                    long androidId = jsonRegInfo.getLong("androidRegistrationId");
                    long userId = jsonRegInfo.getLong("userRegistrationId");
                    String token = jsonRegInfo.get("registrationToken").toString();
    
                    reqResp.androidRegistrationId = androidId;
                    reqResp.registrationToken = token;
                    reqResp.userRegistrationId = userId;
    
                } catch (JSONException jsonEx) {
                    String message = jsonEx.getMessage();
                }
    
                catch (NullPointerException n) {
                    String message = n.getMessage();
                } catch (Exception ex) {
                    String message = ex.getMessage();
                }
                return reqResp;
            }
        }
    

    此代码向 WCF Web 服务发出 JSon 请求并获得 JSon 响应,该响应最终被解析为特定对象,然后返回。

    public class CSRHttpClient {
    
        public static HttpClient getNewHttpClient()
        {
            try
            {
                KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
                trustStore.load(null, null);
    
                SSLSocketFactory sf = new CSRSSLSocketFactory(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);
    
                SchemeRegistry registry = new SchemeRegistry();
                registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
                registry.register(new Scheme("https", sf, 443));
    
                ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
    
                return new DefaultHttpClient(ccm, params);
            } catch (Exception ex)
            {
                return new DefaultHttpClient();
            }
    
        }
    
    
    }
    

    此类仅用于实例化自定义套接字工厂,它允许接受所有有效和无效的服务器证书。不建议在敏感信息服务/传输上采用此类做法,因为接受所有有效证书会导致中间人攻击,就像其他一些漏洞一样。

    希望对你有所帮助。

    祝你好运。

    【讨论】:

    • 好吧,就我而言,我有基于球衣的 Web 服务器,它接受 xml 中的参数和 xml 中的给予者 sessionId
    • CSRHttpClient是继承自HttpClient的自定义HttpClient。
    • 输入/输出格式无关紧要,因为您只需要使用序列化参数调用服务器,我猜。我将尝试编辑响应以包含 CSRHttpClient 类。你能发布一些你的代码,这样我就可以了解你想要实现的目标吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    • 2020-06-16
    • 2014-04-06
    • 2013-10-28
    • 2013-09-02
    相关资源
    最近更新 更多