HTTP client - HTTP 405 error "Method not allowed". I send a HTTP Post but for some reason HTTP Get i

Posted by Shino88 on Stack Overflow See other posts from Stack Overflow or by Shino88
Published on 2010-03-22T19:34:34Z Indexed on 2010/03/22 19:41 UTC
Read the original article Hit count: 1138

Filed under:
|
|
|
|

Hey

I am using 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" which i fixed by adding paramenters.

below is my class with the post method.

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");

            }
         });

© Stack Overflow or respective owner

Related posts about httpclient

Related posts about apache