0
HttpClient myClient = new HttpClient();
myClient.BaseAddress = new Uri(URL);

Base address already specified on client it's URL.

var encodedObject = JsonConvert.SerializeObject(Obj);
myClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await myClient.PostAsync("test.php/yourAPI",new StringContent(encodedObject, System.Text.Encoding.UTF8, "application/json"));
if (response.IsSuccessStatusCode)
{
  var responseContent =  response.ToString();
  var responsebody = response.Content.ToString();
  Stream receiveStream = response.GetResponseStream();
  string responseBodyAsText = response.Content.ReadAsStringAsync().Result;
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Are you in control of the server side code as well? – Nkosi Jul 05 '16 at 12:02
  • In away Yes ! it's not a Public server ! to answer your question – Mohamed Aymen Haddad Jul 05 '16 at 12:12
  • In a lot of these cases the server is throwing an error and defaulting to the error page (html). the endpoint you are calling. Is it a web api or normal Asp.Net MVC – Nkosi Jul 05 '16 at 12:17
  • I have tested the API with POSTMAN and it's working fine, still it's not the case with Xamarin. Additionnaly "response" get a response code 200 OK but the content is NO JSON it's HTML. I am using a Web API – Mohamed Aymen Haddad Jul 05 '16 at 12:22
  • Try clearing the headers before adding default – Nkosi Jul 05 '16 at 12:29
  • You are probably going to have to show some more details about your setup to get a better assessment of the problem. check how to provide a [mcve] – Nkosi Jul 05 '16 at 12:38
  • You say it works with post man. inspect the request being sent by portman and then inspect/compare the request being sent by your code. – Nkosi Jul 05 '16 at 12:43
  • for more details, it's all my code, I just removed the URL – Mohamed Aymen Haddad Jul 05 '16 at 12:56
  • thats not all the code. its a snippet. you say the client is created and that base address is added. that is not in the code you showed in the example. Show a complete example so that the code can be used to replicate your problem. At this point others will have to guess what you have before the code you have displayed. you can always replace the url with a fake one. – Nkosi Jul 05 '16 at 13:01
  • that's all i implemented so far. – Mohamed Aymen Haddad Jul 05 '16 at 13:36

2 Answers2

0

Could be a content negotiation issue. Try clearing the Accept header before adding the json media type

myClient.DefaultRequestHeaders.Accept.Clear();
myClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//...other code removed for brevity.

this code sets the Accept header to "application/json", which tells the server to send data in JSON format.

Reference source: Calling a Web API From a .NET Client in ASP.NET Web API 2

Nkosi
  • 235,767
  • 35
  • 427
  • 472
0

the Method PostAsync has as first argument the complete URI of The API. Therefore, it should be like follow :

    HttpResponseMessage response = await myClient.PostAsync("http://bla-bla-bla/test.php/test",new StringContent(encodedObject, System.Text.Encoding.UTF8, "application/json"));

And there is no need to define the BaseAddress.