I. Differences between Get requests and Post requests
1. Get is a request to request data from the server, while Post is a request to submit data to the server.
2. Get is used to obtain information rather than modify information. Similar to the database query function, data is not modified.
3. The parameters of the Get request will be transmitted after the url, and the request data will be appended to the URL? Splits the URL and transmits data. The parameters are connected to each other. XX in % XX represents the ASCII code in hexadecimal notation. If the data is an English letter or number, it is sent as is, if it is a space, convert it to +. If it is a Chinese character or other character, the string is encrypted using base64.
4. The size of the data transmitted by Get is limited. Because GET submits data through a URL, the amount of data that can be submitted by GET is directly related to the URL length, different browsers have different restrictions on the URL length.
5. The GET request data will be cached by the browser, and the user name and password will appear in the URL in plain text. Others can check the historical browsing records, which is not safe.
THE Post request is sent to the web server as the actual content of the http message, and the data is placed in the HTML Header for submission. There is no limit on the submitted data for Post.
6. Post is safer than Get. when the data is Chinese or insensitive, get is used. Because get is used, the parameter is displayed at the address, for sensitive and non-Chinese characters, POST and POST are used to indicate requests that may modify resources on the server.
2. Use the GET and POST methods to query the IP Address Source of an application, as shown below:
Iii. implementation process (all files claimed in this article are prefixed with We)
1. The required control has four labels, one of which serves as the output port, one textField serves as the input box of the IP address, and one UIButton serves as the action response button and is associated with WeViewController. h.
# Import
@ Interface WeViewController: UIViewController
{
// Store the received data to a variable-length string
NSMutableString * _ pMutableStr;
}
@ Property (retain, nonatomic) IBOutletUITextField * pTextIP;
@ Property (retain, nonatomic) IBOutletUILabel * pValueLabel;
-(IBAction) buttonPressed :( id) sender;
@ End
2. In WeViewController. m, take one of the get and post methods.
-(IBAction) buttonPressed :( id) sender
{
/* // GET request
// Obtain the IP address in the current textField
NSString * pStr = self. pTextIP. text;
// Concatenate the string
NSString * strURL = [@ "http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx/getCountryCityByIp? TheIpAddress = "stringByAppendingString: pStr];
// Convert to URL
NSURL * pURL = [NSURL URLWithString: strURL];
// Create a request
NSURLRequest * pRequest = [NSURLRequest requestWithURL: pURL cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 60];
// Initiate a request to the server
[NSURLConnection connectionWithRequest: pRequest delegate: self];
*/
// POST
// The first difference from the Get request (without parameters, the parameter attachment is in the body)
NSString * postStr = @ "http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx/getCountryCityByIp ";
// Convert to URL
NSURL * postURL = [NSURLURLWithString: postStr];
// Second difference (the request is NSMutableURLRequest)
NSMutableURLRequest * postRequest = [NSMutableURLRequestrequestWithURL: postURL cachePolicy: NSURLRequestUseProtocolCachePolicytimeoutInterval: 60];
// Make the parameter into a string
NSString * post1 = [NSStringstringWithFormat: @ "theIpAddress = % @", self. pTextIP. text];
// Convert to NSData
NSData * postData = [post1dataUsingEncoding: NSUTF8StringEncoding];
// The third difference (using the parameter as the Body)
[PostRequestsetHTTPBody: postData];
// Point 4 (the current request method must be manually declared as POST)
[PostRequestsetHTTPMethod: @ "POST"];
// Send a request to the server
[NSURLConnectionconnectionWithRequest: postRequest delegate: self];
}
3. Delegate method implementation
# Pragma mark ------ URL delegate ----
-(Void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response
{
NSLog (@ "server response ");
_ PMutableStr = [[NSMutableStringalloc] init];
}
-(Void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data
{
NSLog (@ "receive data ");
NSString * pStr = [[NSStringalloc] initWithData: dataencoding: NSUTF8StringEncoding];
[_ PMutableStrappendString: pStr];
NSLog (@ "pStr = % @", _ pMutableStr );
}
-(Void) connectionDidFinishLoading :( NSURLConnection *) connection
{
NSLog (@ "received data succeeded ");
NSMutableString * pValue = [[NSMutableStringalloc] init];
// Obtain the Chinese character part of the string
For (int I = 0; I <[_ pMutableStrlength]; I ++)
{
Int a = [_ pMutableStrcharacterAtIndex: I];
If (a> 0x4e00 & a <0x9fff)
{
[PValue appendString: [_ pMutableStrsubstringWithRange: NSMakeRange (I, 1)];
}
}
NSLog (@ "pValue = % @", pValue );
Self. pValueLabel. text = pValue;
[PValuerelease];
}
-(Void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error
{
NSLog (@ "Error = % @", [errorlocalizedDescription]);
}
// Method for removing the keyboard
-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event
{
[Self. viewendEditing: YES];
}
4. So far, the function has been implemented. To beautify the interface, you can add the following code to add a background image to the interface.
-(Void) viewDidLoad
{
[SuperviewDidLoad];
UIImageView * pImageView = [[UIImageViewalloc] initWithFrame: self. view. frame];
[PImageViewsetImage: [UIImageimageNamed: @ "new"];
[Self. viewinsertSubview: pImageView atIndex: 0];
}
NOTE: For the web service, visit http://www.webxml.com.cn/zh_cn/web_services.aspxto find the xmlresource in the terminal.