这是我的HTTP JSON
{
"User": {
"Name": "Compulsary","Password": "Compulsary" }
}
Run Code Online (Sandbox Code Playgroud)
我必须使用HTTPS POST请求发布到服务器.当我尝试发送请求时,我收到SSL服务器证书异常.我该如何解决这个问题?
我试过了什么?
我已经尝试使用HTTPPost带HTTPClient.SSL异常.我也试过使用URLConnection并忽略了SSL证书的检查.我得到401和405响应代码.
问题陈述:
将上述POST请求发送到服务器(请求是安全的,接受https).如何在android中实现这一点?
我试过这个,我的代码,我得到405错误?
// always verify the host - dont check for certificate
final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
/**
* Trust every server - dont check for any certificate
*/
private static void trustAllHosts() {
// Create a trust manager that does not validate certificate chains …Run Code Online (Sandbox Code Playgroud) 这是我向服务器发出 POST 请求的代码。
要发布到服务器的 JSON :
{
"User": {
"Name": "dog","Password": "123" }
}
Run Code Online (Sandbox Code Playgroud)
我如何创建 JSON 对象
object = new JSONObject();
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("Name", "dog");
jsonObject.put("Password", "123");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
object.put("User", jsonObject);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
发布到服务器作为
public HttpResponse request(JSONObject request)
throws ClientProtocolException, IOException, IllegalStateException,
JSONException {
client = (DefaultHttpClient) WebClientDevWrapper
.getNewHttpClient();
HttpPost post = new …Run Code Online (Sandbox Code Playgroud) android ×2