Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following code describes a simple example using <code>POST</code> method.(<em>How one can pass data by <code>POST</code> method</em>) </p> <p>Here, I describe how one can use of POST method. </p> <p><strong>1.</strong> Set post string with actual username and password.</p> <pre><code>NSString *post = [NSString stringWithFormat:@"Username=%@&amp;Password=%@",@"username",@"password"]; </code></pre> <p><strong>2.</strong> Encode the post string using <code>NSASCIIStringEncoding</code> and also the post string you need to send in NSData format. </p> <pre><code>NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; </code></pre> <p>You need to send the actual length of your data. Calculate the length of the post string.</p> <pre><code>NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; </code></pre> <p><strong>3.</strong> Create a Urlrequest with all the properties like <code>HTTP</code> method, http header field with length of the post string. Create <code>URLRequest</code> object and initialize it. </p> <pre><code>NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; </code></pre> <p>Set the Url for which your going to send the data to that request.</p> <pre><code>[request setURL:[NSURL URLWithString:@"http://www.abcde.com/xyz/login.aspx"]]; </code></pre> <p>Now, set <strong>HTTP</strong> method (<em>POST or GET</em>). Write this lines as it is in your code.</p> <pre><code>[request setHTTPMethod:@"POST"]; </code></pre> <p>Set <code>HTTP</code> header field with length of the post data. </p> <pre><code>[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; </code></pre> <p>Also set the Encoded value for HTTP header Field.</p> <pre><code>[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; </code></pre> <p>Set the <code>HTTPBody</code> of the urlrequest with postData.</p> <pre><code>[request setHTTPBody:postData]; </code></pre> <p><strong>4.</strong> Now, create URLConnection object. Initialize it with the URLRequest.</p> <pre><code>NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; </code></pre> <p>It returns the initialized url connection and begins to load the data for the url request. You can check that whether you <code>URL</code> connection is done properly or not using just <strong>if/else</strong> statement as below.</p> <pre><code>if(conn) { NSLog(@"Connection Successful"); } else { NSLog(@"Connection could not be made"); } </code></pre> <p><strong>5.</strong> To receive the data from the HTTP request , you can use the delegate methods provided by the URLConnection Class Reference. Delegate methods are as below. </p> <pre><code>// This method is used to receive the data which we get using post method. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data // This method receives the error report in case of connection is not made to server. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error // This method is used to process the data after connection has made successfully. - (void)connectionDidFinishLoading:(NSURLConnection *)connection </code></pre> <p><strong>Also Refer</strong> <a href="http://deusty.blogspot.in/2006/11/sending-http-get-and-post-from-cocoa.html" rel="noreferrer">This</a> <strong>and</strong> <a href="http://yuvarajmanickam.wordpress.com/2012/10/17/nsurlconnection-basics-for-ios-beginners/" rel="noreferrer">This</a> <strong>documentation</strong> for <code>POST</code> method. </p> <p>And here is best example with source code of <a href="https://web.archive.org/web/20150315072521/http://kemal.co/index.php/2012/02/fetching-data-with-getpost-methods-by-using-nsurlconnection/" rel="noreferrer">HTTPPost Method.</a></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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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