1

我正在使用 apache 库。我创建了一个向 servlet 发送发布请求的类。我已经为客户端设置了参数,并且我创建了一个要发送的 HTTP post 对象,但是由于某种原因,当我执行请求时,我得到一个回复​​,说不支持 get 方法(这是真的,因为我只做了我的 servlet 中的一个 dopost 方法)。似乎正在发送获取请求,但我不知道为什么。post方法以前有效,但由于我没有设置协议版本,我开始gettng 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");

            }
         });
4

1 回答 1

1
  1. 你怎么知道 aHTTP GET真的被发送了?检查客户端发送或服务器接收的 http 数据包在这里会有所帮助
  2. 在构造 Post 的参数时捕获并吞下 UnsupportedEncodingException - 如果在设置参数时遇到此异常会发生什么?您今天的代码仍将尝试执行POST.
于 2010-03-22T19:40:02.120 回答