tags:

views:

452

answers:

4

For a GET request I've tried this simple method:

      NSString *urlAddress = @"http://example.com/";
      NSURL *url = [NSURL URLWithString:urlAddress];
      NSURLRequest *request = [NSURLRequest requestWithURL:url];
      [uiWebViewThingy loadRequest:request];

(Although it doesn't seem to work if the request contains high UTF-8 characters. )

I want to send a POST from the iPhone.

This also give an overview for sending POST/GET requests although what I really want to do is embed the resulting web page into UIWebView. What would be the best way to go around that?

A: 

Using loadHTMLString, feed a page to the UIWebView that has a hidden, pre-populated form, then make that page do a Javascript forms[0].submit() on loading.

EDIT: First, you collect the input into variables. Then you compose HTML thusly:

NSMutableString *s = [NSMutableString stringWithCapacity:0];
[s appendString: @"<html><body onload=\"document.forms[0].submit()\">"
 "<form method=\"post\" action=\"http://someplace.com/\"&gt;"
 "<input type=\"hidden\" name=\"param1\">"];
[s appendString: Param1Value]; //It's your variable
[s appendString: @"</input></form></body></html>"];

Then you add it to the WebView:

[myWebView loadHTMLString:s baseURL:nil];

It will make the WebView load the form, then immediately submit it, thus executing a POST request to someplace.com (your URL will vary). The result will appear in the WebView.

The specifics of the form are up to you...

Seva Alekseyev
I don't really understand this. I'm trying to send a POST request from the iPhone based on a users input and have the server produce a page which in turn will appear inside the UIWebView.
Gazzer
+3  A: 

You can use something like ASIHTTPRequest to make the POST request (With the option of doing it asynchronously) and then load the response string/data into the UIWebView. Look at this page under the section titled Sending data with POST or PUT requests and then look at the Creating an asynchronous request section at the top for information on how to handle the response string/data.

Hope that helps, sorry if I misunderstood your question.

Jorge Israel Peña
+2  A: 

You can use an NSMutableURLRequest, set the HTTP method to POST, and then load it into your UIWebView using -loadRequest.

Ben Gottlieb
This is better.
Seva Alekseyev
This is what I tried (see the link in the OP) and I tried to use the connectionDidFinishLoading method to do something like [uiWebViewThing loadRequest:responseData] but it's the wrong kind of data. The response data is NSMutable but it needs to be NSUrlRequest - I must be doing something obvious wrong.
Gazzer
The code above uses an NSURLRequest. You need to use an NSMutableURLRequest. Don't mess around with connectionDidFinishLoading, just load it into the webView using -loadRequest.
Ben Gottlieb
A: 

You can get a very good article about HTTP requests here

Nithin