HTTP 客户端 - HTTP 405 错误“方法不允许”。我发送了 HTTP Post,但由于某种原因发送了 HTTP Get
我正在使用 apache 库。我创建了一个向 servlet 发送 post 请求的类。我已经为客户端设置了参数,并且创建了一个要发送的 HTTP post 对象,但由于某种原因,当我执行请求时,我得到一个响应,表示不支持 get 方法(这是真的,因为我只做了我的 servlet 中的 dopost 方法)。似乎正在发送一个 get 请求,但我不知道为什么。 post 方法之前有效,但由于我没有设置协议版本,我开始获取 http 错误 417“期望失败”,但我通过添加参数修复了此问题。
下面是我的类,您可以在其中看到正在创建和执行的 HTTPpost 对象。我有一个响应处理程序方法,但我将其从下面的代码中取出,因为它与我的问题无关。
我知道正在发送 HTTP GET,因为返回的响应消息显示。所请求的资源不允许指定的 HTTP 方法(此 URL 不支持 HTTP 方法 GET)。
提前致谢。
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() );
}
}
}
我尝试将允许标头添加到请求中,但效果不佳,但我不确定我做得是否正确。下面是代码。
client.addRequestInterceptor(new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext context)
throws HttpException, IOException {
//request.addHeader("Allow", "POST");
}
});
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.
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.
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).
Thanks in advance.
P.s i am developing for 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");
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HTTP GET
实际上正在发送?检查客户端发送的或服务器接收的http数据包在这里会很有帮助。POST
。HTTP GET
is actually being sent? Examining the http packets sent by your client or received by the server would be helpful herePOST
.