views:

441

answers:

1

Hey

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

            }
         });
+1  A: 
  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.
matt b
1. The response message says that a get is being sent. Also how would i check the http packets.2. Which parameters have these exception that need to be caught. Thanks
Shino88
For 1, you should rule out that the server is sending an incorrect error message which is confusing the situation. Check the access log (if any) on the server, or you can enable verbose logging within httpclient to see all the data it sends across the wire - or use a packet sniffing tool like WireShark.
matt b
Hey matt thanks for the help. I'm a student at Uni and i dont have access to the server logs I just tried wireshark but it ketp crashing and to tell you the truth i dont even know what im lookin for. Any ideas on what it could be. O and i keep getting The specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL) response from the server.
Shino88