MaAc
MaAc

Reputation: 281

Consume WCF Restful Service in Android through HttpUrlConnection, Getting error 405-Method Not Allowed

I write a simple WCF Restful web service in C# to get some value against some parameters. Here is my IService code:

[OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped)]
    string GetURL(string pVr);

And this is the code for Service.svc

public string GetURL(string pVr)
    {
        try
        {
            string VersionCode = ConfigurationManager.AppSettings["MobAppVersionCode"];
            if (Convert.ToInt32(VersionCode) > Convert.ToInt32(pVr))
            {
                return "http://xxx.xxx.xxx.xxx/abc.apk";
            }
            else
            {
                return "No apk available";
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

and one of my purpose is when I will publish this web service into IIS the method should be hidden (no more rest or help page or wsdl) so that i changed the web config and set like this

<behaviors>
  <serviceBehaviors>
    <behavior name="SEILServiceBehaviour">
      <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
      <serviceDebug includeExceptionDetailInFaults="false" httpHelpPageEnabled="false" httpsHelpPageEnabled="false" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp helpEnabled="false"/>
    </behavior>
  </endpointBehaviors>
</behaviors>

Now after publishing this web service into IIS i can easily see the service result through postman or by web browser, or even I can fetch the data through Retrofit2 android library. but when I am connected this web service with my HttpUrlConnection code, it will show me the error 405-Method Not Allowed. Here is the code of my connection page

public void onRequest(String urlParameters) {
    try {
        //Establishing connection with particular url
        URL url = new URL("http://xxx.xxx.xxx.xxx/Service.svc/GetURL");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        //Setting connection timeout
        connection.setReadTimeout(30000);
        connection.setConnectTimeout(30000);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
        connection.setRequestProperty("Content-Language", "en-US");
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();

        InputStream inputStream;

        int status = connection.getResponseCode();

        if (status != HttpURLConnection.HTTP_OK)
            inputStream = connection.getErrorStream();
        else
            inputStream = connection.getInputStream();

        BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
        }
        rd.close();

        if (url.getHost().equals(connection.getURL().getHost())) {
            urlReceive = response.toString().trim();
        } else {
            urlReceive = "Internet Login Required";
        }
    } catch (IOException e) {
        urlReceive = "Connection Timed Out";
    }
}

Please help me

Upvotes: 0

Views: 314

Answers (1)

mjwills
mjwills

Reputation: 23898

Your request needs to be a GET, as per:

[WebInvoke(Method = "GET",

You are using a POST:

connection.setRequestMethod("POST");

You need to change your code to use GET request rather than a POST.

Upvotes: 1

Related Questions