从Android设备访问WCF Rest服务(Method ='POST')

时间:2014-07-17 00:46:18

标签: android wcf rest http-post

我在部署服务器上托管了一个wcf rest服务,我试图从Android设备调用它。我已经在互联网上尝试了几乎所有的东西,但它仍然没有工作,它一直给我一个错误'端点未找到'。以下是我的代码段

Android代码:

            string url = "http://productionservertmp.com/Service1.svc/PostServiceRequest";
            HttpPost httpPost = new HttpPost(url);    

            JSONObject jobj = new JSONObject();
            jobj.put("reqobj", "Greetings from Android client");
            StringEntity se = new StringEntity( jobj.toString() );
            httpPost.setEntity(se);

            HttpClient client = new DefaultHttpClient();
            response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();
            String data = null;
            JSONArray jsonResponse = null;
            try {
                  data = EntityUtils.toString(entity); //Here's where i find out about response (endpoint not found)
                  jsonResponse = new JSONArray(data);
             } catch (Exception e) {}


            //Signature of my function in interface:
            [OperationContract]
            [WebInvoke(UriTemplate = "PostServiceRequest", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, Method = "POST")]
            string PostServiceRequest(string reqobj);

           //Method in Implementation class
           public string PostServiceRequest(string reqobj)
           {
               return "Response: "+reqobj;
           }

           //And in web.config on deployment server, I've following
           <services>
           <service name="Service1">
           <endpoint address="" behaviorConfiguration="restBehavior" bindingConfiguration="" binding="webHttpBinding" contract="IService1">
           </endpoint>
           </service>
           </services>

我在Method =“GET”的同一服务中有另一个函数,并且已成功调用它。问题在于POST方法。我错过了什么?

1 个答案:

答案 0 :(得分:0)

首先,

您的服务网址有问题:

http://productionservertmp.com/Service1.svc/PostServiceRequest

浏览器无法访问它。确保它有效。

第2点: 使用[WebInvoke]正确配置服务,因为您有要发送的服务参数。

[WebInvoke(UriTemplate =&#34; / PostServiceRequest / {reqobj}&#34;,ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Bare,RequestFormat = WebMessageFormat.Json,Method =&#34; POST&#34 )]

尝试这些更改。