通过HttpUrlConnection在Android中使用WCF Restful Service,获取错误405-方法不允许

时间:2017-11-05 19:01:06

标签: c# android rest web-services wcf

我在C#中编写了一个简单的WCF Restful Web服务来获取某些参数的值。这是我的IService代码:

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

这是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;
        }
    }

我的目的之一就是当我将这个Web服务发布到IIS时,应隐藏该方法(不再有休息或帮助页面或wsdl),以便我更改了Web配置并设置为这样

<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>

现在将此Web服务发布到IIS后,我可以通过邮递员或Web浏览器轻松查看服务结果,甚至可以通过Retrofit2 android库获取数据。但是当我使用我的HttpUrlConnection代码连接此Web服务时,它将显示错误405-Method Not Allowed。这是我的连接页面的代码

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

请帮帮我

1 个答案:

答案 0 :(得分:1)

您的请求必须是GET,按照:

[WebInvoke(Method = "GET",

您使用的是POST

connection.setRequestMethod("POST");

您需要更改代码以使用GET请求而不是POST