通过跳过 SSL 证书连接到 https 服务器时(我的意思是允许所有主机)。登录到这样的 https 服务器后,我们是否需要每次都登录才能触发 get 或 post 请求。
我正在android中尝试这个。任何好的指针都会有所帮助。
使用 httpclient 登录到 https 服务器(通过允许全部跳过 SSL)在登录后触发简单的 Get 请求。
对于这个简单的场景,是否有任何示例代码库。
首先,您必须将代码放在异步任务中,因为网络调用不在主线程上运行。
然后你可以像这样使用它:
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();
}
}
}
此类仅用于实例化自定义套接字工厂,它允许接受所有有效和无效的服务器证书。不建议在敏感信息服务/传输上采用此类做法,因为接受所有证书为有效证书允许中间人攻击,就像其他一些漏洞一样。
希望这对您有所帮助。
祝你好运。