Sending HTTP requests for IOS4.2 Network Learning

Source: Internet
Author: User
Tags response code

IOS4.2Sending of Network LearningHTTP RequestYes. What I want to introduce in this article is mainly about sendingHTTP RequestLet me learn about the IOS-related network content. Let's take a look at the details first.

Development Environment client: mac OS x 10.6.6, ios 4.2 + xcode3.2.5 server: windows xp + iis + asp.net

The Code is as follows:

 
 
  1. -(IBAction) sendHttp: (id) sender {
  2. // Send an http request through GET
  3. // NSUTF8StringEncoding encoding is required for any Chinese Characters
  4.  
  5. NSString * urlString = [[NSString stringWithFormat: @ "http: // 127.0.0.1/default. aspx? Uc = % @ ", @" test"
  6. StringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
  7.  
  8. // Initialize the http request and release the memory automatically
  9.  
  10. NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init] autorelease];
  11.  
  12. [Request setURL: [NSURL URLWithString: urlString];
  13. [Request setHTTPMethod: @ "GET"];
  14.  
  15. NSString * contentType = [NSString stringWithFormat: @ "text/xml"];
  16. [Request addValue: contentType forHTTPHeaderField: @ "Content-Type"];
  17.  
  18. NSHTTPURLResponse * urlResponse = nil;
  19. NSError * error = [[NSError alloc] init];
  20.  
  21. // Synchronously return the request and obtain the returned data
  22.  
  23. NSData * responseData = [NSURLConnection sendSynchronousRequest: request returningResponse: & urlResponse error: & error];
  24. NSString * result = [[NSString alloc] initWithData: responseData encoding: NSUTF8StringEncoding];
  25. // The return status of the request. If the request cannot be sent in Chinese, the value of stausCode is 0.
  26. NSLog (@ "response code: % d", [urlResponse statusCode]);
  27. If ([urlResponse statusCode]> = 200 & [urlResponse statusCode] <300 ){
  28. NSLog (@ "response: % @", result );
  29. Messag. text = [NSString stringWithFormat: @ "% @", result];
  30. }
  31. }

Server:

The content of the default. aspx file is as follows (clear the automatically generated file and only save the following content)

 
 
  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %> 

Default. aspx. cs File

Add the following code in Page_Load mode:

 
 
  1. if (Request.Params["uc"] != null )  
  2. {  
  3.     string userAcount  = Request.Params["uc"];  
  4.     Response.Write("success" + userAcount );  
  5. }  
  6. else  
  7. {  
  8.     Response.Write("fail");  

OK. After the code has been compiled, publish the iis Site for testing.

Summary:IOS4.2Sending of Network LearningHTTP RequestI hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.