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:
- -(IBAction) sendHttp: (id) sender {
- // Send an http request through GET
- // NSUTF8StringEncoding encoding is required for any Chinese Characters
-
- NSString * urlString = [[NSString stringWithFormat: @ "http: // 127.0.0.1/default. aspx? Uc = % @ ", @" test"
- StringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
-
- // Initialize the http request and release the memory automatically
-
- NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init] autorelease];
-
- [Request setURL: [NSURL URLWithString: urlString];
- [Request setHTTPMethod: @ "GET"];
-
- NSString * contentType = [NSString stringWithFormat: @ "text/xml"];
- [Request addValue: contentType forHTTPHeaderField: @ "Content-Type"];
-
- NSHTTPURLResponse * urlResponse = nil;
- NSError * error = [[NSError alloc] init];
-
- // Synchronously return the request and obtain the returned data
-
- NSData * responseData = [NSURLConnection sendSynchronousRequest: request returningResponse: & urlResponse error: & error];
- NSString * result = [[NSString alloc] initWithData: responseData encoding: NSUTF8StringEncoding];
- // The return status of the request. If the request cannot be sent in Chinese, the value of stausCode is 0.
- NSLog (@ "response code: % d", [urlResponse statusCode]);
- If ([urlResponse statusCode]> = 200 & [urlResponse statusCode] <300 ){
- NSLog (@ "response: % @", result );
- Messag. text = [NSString stringWithFormat: @ "% @", result];
- }
- }
Server:
The content of the default. aspx file is as follows (clear the automatically generated file and only save the following content)
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
Default. aspx. cs File
Add the following code in Page_Load mode:
- if (Request.Params["uc"] != null )
- {
- string userAcount = Request.Params["uc"];
- Response.Write("success" + userAcount );
- }
- else
- {
- 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!