我的 Android 应用程序应该与用C#.Net. 从手机发送的数据包含不应向公众公开的数据。所以我正在尝试使用该https协议。在我的服务器端,我要求所有请求都是HTTPS,如下所示:
RequireRegisteredImei
public class RequireRegisteredImeiAttribute : AuthorizationFilterAttribute
    {
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            var request = actionContext.ControllerContext.Request;
            if (request.RequestUri.Scheme == Uri.UriSchemeHttps)
            {
               //OKAY
            }
            else 
            {
             actionContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden);
            }
        }
     }
在控制器中:
[RequireRegisteredImei]
public string Post()
{
}
我通过从我的手机发送一个简单的来调试这段代码http request,这段代码运行得很好,它会拒绝我。
所以,我开始研究如何https从我的安卓手机发送请求。我想出了这样的事情:
public static DefaultHttpClient getSecureHttpClient() {
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
        schemeRegistry.register(new Scheme("http", SSLSocketFactory.getSocketFactory(), 80));
        BasicHttpParams params = new BasicHttpParams();
        SingleClientConnManager mgr = new SingleClientConnManager(params, schemeRegistry);
        return new DefaultHttpClient(mgr, params);
    }
我以这种方式使用这种方法:
HttpClient httpClient = CustomHttpClient.getSecureHttpClient();
这只会导致IOException: No peer certificate
我已经阅读了几个关于此的主题:
Android 2.3 中的“无对等证书”错误,但 4 中没有
但是必须有一种更简单的方法来HTTPS从 android 发布数据?