Android HTTPPost Returns Error "Method not allowed." Android HTTPPost Returns Error "Method not allowed." json json

Android HTTPPost Returns Error "Method not allowed."


Putting a '/' at the end of URL causes the redirect to happen because your server likes urls that end in '/'. POST is fully supported by the URL your server redirects you to, but the client is executing a GET request when it behaves according to your setRedirecting() call (cURL does the same exact thing with the -L switch) The fix is to either put a '/' at the end of URL, or to grab the Location header from the response yourself and then initiate another POST request manually.

This can be observed in wireshark. You can test the theory by trying to perform a GET request with your browser to the URL with a slash appended to it. That will cause the browser to get a 405. Here's the fixed code for Android, this code uses the simple fix of appending a '/' to the URL (not production ready):

 package com.altaver.demo;import java.io.IOException;import java.io.UnsupportedEncodingException;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.HttpResponseException;import org.apache.http.client.methods.HttpPost;import org.apache.http.client.params.HttpClientParams;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.BasicResponseHandler;import org.apache.http.impl.client.DefaultHttpClient;import org.json.JSONException;import org.json.JSONObject;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.widget.Toast;public class AltaVerDemoActivity extends Activity {    private static final String TAG = "MainActivity";    private static final String URL = "http://96.56.2.188/sdz/avReSTfulLogin1/";    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        JSONObject jsonObjSend = new JSONObject();        try {            jsonObjSend.put("Pass", "sz");            jsonObjSend.put("User", "szechman");        } catch (JSONException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        HttpClient client = new DefaultHttpClient();        HttpPost httpPostRequest = new HttpPost(URL);        httpPostRequest.setHeader("User-Agent", "com.altaver.android_PostJson2");        httpPostRequest.setHeader("Accept", "application/json");        httpPostRequest.setHeader("Content-Type", "application/json");        StringEntity se = null;        try {            se = new StringEntity(jsonObjSend.toString());        } catch (UnsupportedEncodingException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        httpPostRequest.setEntity(se);        HttpResponse response = null;        try {            response = client.execute(httpPostRequest);        } catch (ClientProtocolException e) {            // TODO Auto-generated catch block            Toast.makeText(getApplicationContext(),                    "Please check your internet connection",                    Toast.LENGTH_SHORT).show();            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        BasicResponseHandler responseHandler = new BasicResponseHandler();        String strResponse = null;        if (response != null) {            try {                strResponse = responseHandler.handleResponse(response);            } catch (HttpResponseException e) {                // TODO Auto-generated catch block                e.printStackTrace();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        Log.e("AltaVerDemoActivity", "Response: " + strResponse);    }}


The above issue often occurs if the request type in the service is WebGet e.g

WebGet(UriTemplate = "login/?name={name}&password={password}", ResponseFormat = WebMessageFormat.Json)"

and you try to access the method using HttpPost via android.

I have the same issue and it took me hours just to figure it out.


I Watched The Above Answers And Seems A Little Too Much Complicated!!

All I Did To Solve This(Which As Some Of The Answers States, Is Due The Service That Doesn't Allow The POST), Is To Change:

HttpPost post = new HttpPost(params[0]);HttpResponse response = client.execute(post);

For

HttpGet get = new HttpGet(params[0]);HttpResponse response = client.execute(get);

And That Solved It!

PD: I Was Getting Status Code: 405!