Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to POST to WCF service using Android client
    primarykey
    data
    text
    <p>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.</p> <p>Below is the code from the WCF service:</p> <pre><code> [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; } } </code></pre> <p>Below is the code from the Android client:</p> <pre><code> HttpClient httpClient = new DefaultHttpClient(); HttpPost request = new HttpPost("http://xxx.xxx.x.x:8000/SetValue"); List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;(1); params.add(new BasicNameValuePair("something", "12345")); request.setEntity(new UrlEncodedFormEntity(params)); HttpResponse response = httpClient.execute(request); </code></pre> <p>The following is how I start the self-hosted service:</p> <pre><code> 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(); } } } </code></pre> <p>I only have an app.config which just has:</p> <pre><code> &lt;?xml version="1.0"?&gt; &lt;configuration&gt; &lt;startup&gt;&lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/&gt;&lt;/startup&gt;&lt;/configuration&gt; </code></pre> <p>The response I'm getting when I run httpClient.execute(request) from the Android client includes:</p> <pre><code> HTTP/1.1 400 Bad Request Request Error The server encountered an error processing the request. See server logs for more details. </code></pre> <p>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.)</p> <p>[EDIT]</p> <p>I have also tried </p> <pre><code> 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); </code></pre> <p>which also results in the error. </p> <p>I also noticed that if I change 'SetValue' to return a constant, such as "abcd", instead of someValue.Something.ToString(), then everything works?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload