apache HTTP 客户端 - HTTP 405 错误“方法不允许”。我发送了一个 HTTP Post 但由于某种原因发送了 HTTP Get
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2495135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTP client - HTTP 405 error "Method not allowed". I send a HTTP Post but for some reason HTTP Get is sent
提问by Shino88
I am using the apache library. I have created a class which sends a post request to a servlet. I have set up the parameters for the client and i have created a HTTP post object to be sent but for some reason when i excute the request i get a reposnse that says the get method is not supported(which is true cause i have only made a dopost method in my servlet). It seems that a get request is being sent but i dont know why. The post method worked before but i started gettng http error 417 "Expectation Failed" due to me not setting the protocal version but i fixed this by adding paramenters.
我正在使用 apache 库。我创建了一个向 servlet 发送 post 请求的类。我已经为客户端设置了参数,并且我已经创建了一个要发送的 HTTP post 对象,但是由于某种原因,当我执行请求时,我得到一个 reposnse,说不支持 get 方法(这是真的,因为我只做了我的 servlet 中的一个 dopost 方法)。似乎正在发送获取请求,但我不知道为什么。post方法之前有效,但由于我没有设置协议版本,我开始gettng http错误417“预期失败”,但我通过添加参数解决了这个问题。
below is my class where you see a HTTPpost object being created and exectued. I have a response handler method but i took it out of my code below because it has nothing to do with my problem.
下面是我的课程,您可以在其中看到正在创建和执行的 HTTPpost 对象。我有一个响应处理程序方法,但我从下面的代码中删除了它,因为它与我的问题无关。
I know a HTTP GET is being sent because of the reponse mesage that is returned says. The specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL).
我知道由于返回的响应消息说正在发送 HTTP GET。请求的资源不允许指定的 HTTP 方法(此 URL 不支持 HTTP 方法 GET)。
Thanks in advance.
提前致谢。
P.s i am developing for android.
Ps 我正在为 android 开发。
public class HTTPrequestHelper {
private final ResponseHandler<String> responseHandler;
private static final String CLASSTAG = HTTPrequestHelper.class.getSimpleName();
private static final DefaultHttpClient client;
static{
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, HTTP.UTF_8);
///params.setParameter(CoreProtocolPNames.USER_AGENT, "Android-x");
params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 15000);
params.setParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(
new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(
new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
client = new DefaultHttpClient(cm,params);
}
public HTTPrequestHelper(ResponseHandler<String> responseHandler) {
this.responseHandler = responseHandler;
}
public void performrequest(String url, String para)
{
HttpPost post = new HttpPost(url);
StringEntity parameters;
try {
parameters = new StringEntity(para);
post.setEntity(parameters);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BasicHttpResponse errorResponse =
new BasicHttpResponse(
new ProtocolVersion("HTTP_ERROR", 1, 1),
500, "ERROR");
try {
client.execute(post, this.responseHandler);
}
catch (Exception e) {
errorResponse.setReasonPhrase(e.getMessage());
try {
this.responseHandler.handleResponse(errorResponse);
}
catch (Exception ex) {
Log.e( "ouch", "!!! IOException " + ex.getMessage() );
}
}
}
I tried added the allow header to the request but that did not work as well but im not sure if i was doing right. below is the code.
我尝试将允许标头添加到请求中,但这并没有奏效,但我不确定我是否做得对。下面是代码。
client.addRequestInterceptor(new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext context)
throws HttpException, IOException {
//request.addHeader("Allow", "POST");
}
});
采纳答案by matt b
- How do you know that a
HTTP GETis actually being sent? Examining the http packets sent by your client or received by the server would be helpful here - You catch and swallow UnsupportedEncodingException when constructing the Post's parameters - what happens if you encounter this exception while setting parameters? Your code as is today will still attempt to execute the
POST.
- 你怎么知道 a
HTTP GET真的被发送了?检查客户端发送或服务器接收的 http 数据包在这里会有所帮助 - 您在构造 Post 的参数时捕获并吞下 UnsupportedEncodingException - 如果在设置参数时遇到此异常会怎样?您今天的代码仍将尝试执行
POST.

