Android 中的安全 HTTP Post
我有一个非常基本的帮助程序类,我用它来完成所有 Http Get/Post 操作。我使用 org.apache.http 库中的 HttpGet、HttpPost 和 HttpClient。我的所有内容都可以通过 HTTP 正常运行,但是当我尝试使用通过 HTTPS 运行的服务时,我在执行请求时会收到 ClientProtocolException。异常中的唯一消息是“服务器无法使用有效的 HTTP 响应进行响应”。
为了进行测试,我使用简单的 html 表单从浏览器发送了完全相同的负载,并使用 RequestBuilder 从 Fiddler2 发送了完全相同的负载。我发送了无效和空的有效负载,甚至发送了带有和不带有标头的上述所有内容,以查看对象构建请求的方式是否存在一些奇怪的情况。
我在测试中使用的所有内容都会为我提供有效的 200 状态 HTTP 响应。如果我给它的东西与它期望的不同,该服务只会给我一个描述错误的结构。
我是否需要向 HttpPost 或 HttpClient 对象添加一些特殊内容以告诉它使用 HTTPS?我是否必须明确告诉它使用不同的端口?
编辑:
我确实为 https 通信注册了错误的套接字工厂。这是我用来使用正确的套接字工厂创建 HttpClient 对象的更新方法,以防将来有人搜索此类问题:
private HttpClient createHttpClient()
{
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(params, true);
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
return new DefaultHttpClient(conMgr, params);
}
I have a pretty basic helper class that I'm using to do all my Http Get/Post stuff. I'm using HttpGet, HttpPost, and HttpClient from the org.apache.http library. All of my stuff works fine over HTTP, but as soon as I tried to consume a service that works over HTTPS, I get a ClientProtocolException when executing the request. The only message in the exception is "The server failed to respond with a valid HTTP response".
To test, I sent the exact same payload from a browser using a simple html form and Fiddler2 using the RequestBuilder. I've sent invalid and empty payloads and even sent all of the above with and without headers to see if there was something funky about the way the objects were building the request.
Everything I've used in testing gives me a valid 200 status HTTP response. The service just gives me a structure describing the error if I give it something other than what it expects.
Is there something special I need to add to the HttpPost or HttpClient object(s) to tell it to use HTTPS? Do I have to explicitly tell it to use a different port?
EDIT:
I indeed registered the wrong socket factory for https communication. Here is the updated method that I use to create my HttpClient object with the correct socket factory just in case someone searches this kind of problem in the future:
private HttpClient createHttpClient()
{
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(params, true);
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
return new DefaultHttpClient(conMgr, params);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道为什么你不能处理 HTTPS。我为自己的应用程序编写了一个帮助程序类,并且能够毫无问题地 GET/POST 到 HTTPS。我将在这里发布代码,也许你可以看看我的代码和你的代码之间是否存在差异。
I'm not sure why you can't handle HTTPS. I wrote a helper class for my own applications and I am able to GET/POST to HTTPS without a problem. I will post the code here and perhaps you can see if there are differences between my code and yours.
由于有些方法已被弃用,难道不应该是这样吗?
我是否应该更改其他内容,例如 HttpVersion?
As some of the methods are deprecated, should'nt it be like this?
Should I change anything else, like HttpVersion?