Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON POST Request on the iPhone (Using HTTPS)
    text
    copied!<p>I have a WCF service hosted and I'm trying to use it within an iPhone app as a JSON POST request. I plan on using the JSON serializer later, but this is what I have for the request:</p> <pre><code> NSString *jsonRequest = @"{\"username\":\"user\",\"password\":\"letmein\"}"; NSLog(@"Request: %@", jsonRequest); NSURL *url = [NSURL URLWithString:@"https://mydomain.com/Method/"]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; [request setHTTPBody: requestData]; [NSURLConnection connectionWithRequest:[request autorelease] delegate:self]; </code></pre> <p>In my data received I'm just printing it to the console:</p> <pre><code>- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSMutableData *d = [[NSMutableData data] retain]; [d appendData:data]; NSString *a = [[NSString alloc] initWithData:d encoding:NSASCIIStringEncoding]; NSLog(@"Data: %@", a); } </code></pre> <p>And within this response I get this message:</p> <pre><code>Data: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;title&gt;Request Error&lt;/title&gt; &lt;style&gt; &lt;!-- I've took this out as there's lots of it and it's not relevant! --&gt; &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="content"&gt; &lt;p class="heading1"&gt;Request Error&lt;/p&gt; &lt;p xmlns=""&gt;The server encountered an error processing the request. Please see the &lt;a rel="help-page" href="https://mydomain.com/Method/help"&gt;service help page&lt;/a&gt; for constructing valid requests to the service.&lt;/p&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Does anyone know why this happens and where I'm going wrong?</p> <p>I've read about using <a href="http://allseeing-i.com/ASIHTTPRequest/" rel="noreferrer">ASIHTTPPost</a> instead but I can't get the demo to run in iOS4 :(</p> <p>Thanks</p> <p>EDIT:</p> <p>I've just used to <a href="http://www.charlesproxy.com/" rel="noreferrer">CharlesProxy</a> to sniff out what's happening when I call from the iPhone (Simulator) and this is what it gave me: <img src="https://i.stack.imgur.com/mSzC7.png" alt="alt text"></p> <p><strike>The only thing strange I've noticed is it says "SSL Proxying not enabled for this host: enable in Proxy Settings, SSL locations". Does anyone know what this means?</strike> (This also happens in my working windows client).</p> <p>I've just ran this on my windows client and the only thing that is different is the Request and Response text which is encrypted. Am I missing something in order to use a secure HTTP connection to do this?</p> <p>NEW EDIT: This works with a non-HTTPS request such as: <a href="http://maps.googleapis.com/maps/api/geocode/json?address=UK&amp;sensor=true" rel="noreferrer">http://maps.googleapis.com/maps/api/geocode/json?address=UK&amp;sensor=true</a>. So I guess the problem is getting the iPhone to send the POST properly over SSL?</p> <p>I am also using these methods:</p> <pre><code>- (void) connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; } [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; } - (void) connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { if([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust]) { return YES; } } </code></pre> <p>This allows me to get this far. If I don't use them I get an error sent back saying:</p> <blockquote> <p>The certificate for this server is invalid. You might be connecting to a server that is pretending to be “mydomain.com” which could put your confidential information at risk.</p> </blockquote>
 

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