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

            }
         });

Comments

@Shino88 work on your accept rate 1st ..

Written by c0mrade

what's accept rate

Written by Shino88

Accepted Answer

  1. How do you know that a HTTP GET is actually being sent? Examining the http packets sent by your client or received by the server would be helpful here
  2. 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.
Written by matt b
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki