0

I have a self-hosted WCF web service running, and an Android client application. I am able to GET or retrieve data from the web service in json format, however I am unable to POST or send any data to the server.

Below is the code from the WCF service:

 [OperationContract]
 [WebInvoke(Method = "POST",
 UriTemplate = "/SetValue",
 RequestFormat = WebMessageFormat.Json,
 ResponseFormat = WebMessageFormat.Json,
 BodyStyle = WebMessageBodyStyle.Wrapped)]
 public string SetValue(TestClass someValue)
 {
     return someValue.Something.ToString();
 }

[DataContract]
public class TestClass
{
    [DataMember(Name = "something")]
    public int Something
    {
        get;
        set;
    }
}

Below is the code from the Android client:

 HttpClient httpClient = new DefaultHttpClient();
 HttpPost request = new HttpPost("http://xxx.xxx.x.x:8000/SetValue");
 List<NameValuePair> params = new ArrayList<NameValuePair>(1);
 params.add(new BasicNameValuePair("something", "12345"));
 request.setEntity(new UrlEncodedFormEntity(params));
 HttpResponse response = httpClient.execute(request);

The following is how I start the self-hosted service:

 class Program
 {
    static void Main()
    {
        Uri baseAddress = new Uri("http://localhost:8000/");

        using (WebServiceHost host = new WebServiceHost(typeof(ServerSideProfileService), baseAddress))
        {
            host.AddServiceEndpoint(typeof(ServerSideProfileService), new BasicHttpBinding(), "Soap");
            ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ServerSideProfileService), new WebHttpBinding(), "Web");
            endpoint.Behaviors.Add(new WebHttpBehavior());

            // Open the service host, service is now listening
            host.Open();
        }
     }
  }

I only have an app.config which just has:

 <?xml version="1.0"?>
 <configuration>
 <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

The response I'm getting when I run httpClient.execute(request) from the Android client includes:

 HTTP/1.1 400 Bad Request
 Request Error
 The server encountered an error processing the request. See server logs for more details.

And that's pretty much it. I am very new to WCF and don't know where this 'server log' would be, and am at a loss as to how to troubleshoot or debug this? (I have tried Fiddler2 but it doesn't seem to detect anything from the Android client.)

[EDIT]

I have also tried

 JSONObject json = new JSONObject(); 
 json.put("something", "12345"); 
 StringEntity entity = new StringEntity(json.toString()); 
 entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
 entity.setContentType( new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));  
 request.setEntity(entity); 

which also results in the error.

I also noticed that if I change 'SetValue' to return a constant, such as "abcd", instead of someValue.Something.ToString(), then everything works?

lost_bits1110
  • 147
  • 1
  • 5
  • 17
  • Try this i hope that this is very help full. [Android HTTPPost Returns Error “Method not allowed.](http://stackoverflow.com/questions/6272381/android-httppost-returns-error-method-not-allowed/6604491#6604491) – Bilal lilla May 21 '12 at 09:50

4 Answers4

2

Your client code is sending a html form post formatted payload, but your server is expecting a json payload, you need the client to be something like

HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost("http://xxx.xxx.x.x:8000/SetValue");
StringEntity e = new StringEntity("{ \"something\":12345 }", "UTF-8");
request.setEntity(e);
request.setHeader("content-type", "application/json");
HttpResponse response = httpClient.execute(request);
superfell
  • 17,629
  • 4
  • 57
  • 79
  • Thanks for your suggestion, unfortunately I still receive the error. I've also tried: `JSONObject json = new JSONObject(); json.put("something", "12345"); StringEntity entity = new StringEntity(json.toString()); entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity.setContentType( new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); request.setEntity(entity);` which also results in the error. I also noticed that if I change 'SetValue' to return a constant ("abcd") instead of someValue.Something.ToString(), then everything works? – lost_bits1110 May 17 '11 at 20:25
  • Then it sounds like the request is making it ok to your code, but your code is throwing an exception. – superfell May 17 '11 at 20:52
  • Okay, you're right. I have added the following 'if statement' in SetValue(TestClass someValue): `if (someValue == null) return "somevalue is null";` and it returns this statement. It looks like the value "12345" specified on the client side is not being passed for some reason :s – lost_bits1110 May 17 '11 at 21:02
  • I think you'll need to find out how the dataContract gets mapped to json. – superfell May 17 '11 at 21:07
  • well, if I change my SetValue definition to: SetValue( string something ) and then return this input string, I see "12345" as expected. At least it works now with simple types, though I am not successful with complex types (i.e. passing class objects) as in the original post. – lost_bits1110 May 17 '11 at 21:45
2

I had the same problem, I resolved this by removing

BodyStyle = WebMessageBodyStyle.WrappedRequest

from my WCF method header

 [WebInvoke(Method = "POST", UriTemplate = "mymethod", RequestFormat=WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
                ResponseFormat=WebMessageFormat.Json)]

Changed to

 [WebInvoke(Method = "POST", UriTemplate = "mymethod", RequestFormat=WebMessageFormat.Json,
                ResponseFormat=WebMessageFormat.Json)]
Hussain
  • 5,446
  • 4
  • 38
  • 50
0
    [WebInvoke(UriTemplate = "crud/delete",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        Method = "POST"
    )]        
    public SampleItem Delete(string id)
    {
        SampleItem item = new SampleItem();
        item.Id = 118;
        item.StringValue = id;
        return item;
        throw new NotImplementedException();
    }


var params  = '{"id":"sfs"}';

function postTest0(){
    $.ajax({
        url:'http://localhost/wcfrest/rest/crud/delete',
        //url:'http://localhost/wcfrest/rest/crud/create', //后台处理程序
        type:'post',    //数据发送方式
        dataType:'json', //接受数据格式
        contentType: "application/json",
        data:params, //要传递的数据
        timeout:1000,
        error:function(){alert('post error');},
        success:update_page //回传函数(这里是函数名)
    });
}

superfell 那个正解!

baozi
  • 1
  • 2
0

From what I have found Android converts the JSON string to a byte array stream and then posts it. Here is my own code as an example

HttpPost httpPost = new HttpPost(URL_Base + uri);
httpPost.setEntity(new StringEntity(sJSONOut));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpEntity oHttpEntity = new DefaultHttpClient().execute(httpPost).getEntity();

In eclipse, when setting a break point on the second to last line and inspecting the httpPost object properties i find the value of my StringEntity is not a byte array [123. 43, 234 ...... even though I have confirmed that my string sJSONOut is correctly formatted json.

Another answer here suggested removing BodyStyle = WebMessageBodyStyle.WrappedRequest from the WCF method header. That gave me the clue I needed to change that line from WrappedRequest to Bare which is what ended up working.

BodyStyle = WebMessageBodyStyle.Bare

masteroleary
  • 924
  • 1
  • 14
  • 32