Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Anyone had problems with sending POST data to a REST web service in IOS7?

$
0
0

Hello,

I have tried two different methods to connect to a REST web service using POST requests from IOS 7:

Method 1: RestSharp

var client = new RestClient ("http://rest_web_service.com/login");
var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(new {Email = username, Password = password});
client.ExecuteAsync (request, response => {                // tried with Execute also; the same issue appear
    // write response to console
});

Method 2: HttpWebRequest

string postDataString = Newtonsoft.Json.JsonConvert.SerializeObject (new {Email = username, Password = password});
byte[] postDataByteArray = Encoding.UTF8.GetBytes (postDataString);

var request = new HttpWebRequest("http://rest_web_service.com/login");
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = postDataByteArray.Length;
request.KeepAlive = false;   // tried with KeepAlive = true also; no effect!

// write the data on the connection stream
Stream dataStream = request.GetRequestStream ();
dataStream.Write (postDataByteArray, 0, postDataByteArray.Length);
dataStream.Close();

HttpWebResponse response = await (HttpWebResponse) request.GetResponseAsync();   // tried with sync request too; same issue
Stream responseStream = response.GetResponseStream();
StreamReader responseReader = new StreamReader(responseStream);
Console.WriteLine("Received: " + responseReader.ReadToEnd());

Everything works fine in the IOS Simulator and on the iphone device if it has been loaded by Xmarin (debug and release versions of the app).

The problem is that if I am building for Ad-Hoc distribution and install the .ipa file on the same iphone device, the POST requests are not working anymore. Using Fiddler to capture the HTTP communication I noticed that the BODY of the request is stripped to {} and the Content-length is set to 2.

All the GET requests are working fine and the REST service is reachable and responds, but I get an error for POST or PUT every time as the body is not sent.

I have also tried with simple communication, without JSON, sending the parameters in the form "Email=mail@mail.com&password=pwd_encription" but in this case the Content-length is set to 0 and the body is really empty. This version works fine in the simulator and on the same device if loaded from Xamarin.

Any ideas of why this is happening?

Thank you!


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>