When iOS POST requests contain Chinese Characters
Hey, some problems with the recent post request. When there is a Chinese character, the request fails.
The following are two examples:
1: Usually post requests: (failure in Chinese)
// 1: Set the URL
NSString * host = HOST;
NSString * usename = @ "fuck ";
NSString * queryString = [NSStringstringWithFormat: @ "/app/clickTableScreen? UserName =%@ & idfa =%@ & operator =%@ & systemVersion =%@ & networkState =%@ & deviceName =%@ & memorySize =%@ & model = % @", usename, adId, [self checkCarrier], phoneVersion, netStr, userPhoneName, totalDiskSpaceStr, [LoginViewControllerdeviceString];
NSLog (@ "querString: % @", queryString );
// Complete set parameters
NSString * urlString = [NSStringstringWithFormat: @ "% @", host, queryString];
NSLog (@ "parameter: % @", urlString );
// Obtain the complete url
NSURL * url1 = [NSURLURLWithString: urlString];
// 2: Request
NSMutableURLRequest * request = [[NSMutableURLRequestalloc] initWithURL: urlcachePolicy: NSURLRequestUseProtocolCachePolicytimeoutInterval: 10];
[Request setHTTPMethod: @ "POST"];
// Step 3: connect to the server
NSError * error = nil;
NSData * reqData = [NSURLConnectionsendSynchronousRequest: request returningResponse: nilerror: & error];
/// 3: Convert the request to binary
// NSData * reqData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
If (reqData = nil ){
If (error) {// This write method has the advantage of listing the cause if the data parsing fails, I did not know the cause because I wrote this sentence: (Error Domain = NSURLErrorDomain Code =-1002 "unsupported URL" UserInfo = 0x14ed24c0 {NSUnderlyingError = 0x)
NSLog (@ "error = % @", error );
}
UIAlertView * alert = [[UIAlertViewalloc] initWithTitle: @ "prompt" message: @ "the network is unstable. Please try again later! "Delegate: nilcancelButtonTitle: @" cancel "otherButtonTitles: @" OK ", nil];
[Alert show];
Return;
} Else {
NSLog (@ "error = % @", error );
}
NSDictionary * reqDic = [NSJSONSerializationJSONObjectWithData: reqData options: NSJSONReadingAllowFragmentserror: nil];
NSString * info = [NSStringstringWithFormat: @ "% @", [reqDicobjectForKey: @ "info"];
NSLog (@ "parameter dictionary: % @", reqDic );
// 4: print data
NSString * errcode = [NSStringstringWithFormat: @ "% @", [reqDicobjectForKey: @ "errcode"];
If ([errcode iscode tostring: @ "0"]) {
NSLog (@ "succeeded ");
} Else
{
UIAlertView * alert = [[UIAlertViewalloc] initWithTitle: @ "failed to get the ad ID" message: info delegate: selfcancelButtonTitle: @ "OK" otherButtonTitles: nil, nil];
[Alert show];
}
}
2: Comparison of the second method (in fact, a sentence is added)
/1: Set the URL
NSString * host = HOST;
NSString * usename = @ "fuck ";
NSString * queryString = [NSString stringWithFormat: @ "/app/clickTableScreen? UserName =%@ & idfa =%@ & operator =%@ & systemVersion =%@ & networkState =%@ & deviceName =%@ & memorySize =%@ & model = % @", usename, adId, [self checkCarrier], phoneVersion, netStr, userPhoneName, totalDiskSpaceStr, [LoginViewController deviceString];
NSLog (@ "querString: % @", queryString );
// Complete set parameters
NSString * urlString = [NSString stringWithFormat: @ "% @", host, queryString];
NSLog (@ "parameter: % @", urlString );
// Obtain the complete url
// Transcoding is required when Chinese characters are available
NSString * urlstr = [urlStringstringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURL * url = [NSURLURLWithString: urlstr];
// 2: Request
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
[Request setHTTPMethod: @ "POST"];
// Step 3: connect to the server
NSError * error = nil;
NSData * reqData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: & error];
/// 3: Convert the request to binary
// NSData * reqData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
If (reqData = nil ){
If (error) {// This write method has the advantage of listing the cause if the data parsing fails, I did not know the cause because I wrote this sentence: (Error Domain = NSURLErrorDomain Code =-1002 "unsupported URL" UserInfo = 0x14ed24c0 {NSUnderlyingError = 0x)
NSLog (@ "error = % @", error );
}
UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "prompt" message: @ "the network is unstable. Please try again later! "Delegate: nil cancelButtonTitle: @" cancel "otherButtonTitles: @" OK ", nil];
[Alert show];
Return;
} Else {
NSLog (@ "error = % @", error );
}
NSDictionary * reqDic = [NSJSONSerialization JSONObjectWithData: reqData options: NSJSONReadingAllowFragments error: nil];
NSString * info = [NSString stringWithFormat: @ "% @", [reqDic objectForKey: @ "info"];
NSLog (@ "parameter dictionary: % @", reqDic );
// 4: print data
NSString * errcode = [NSString stringWithFormat: @ "% @", [reqDic objectForKey: @ "errcode"];
If ([errcode iscode tostring: @ "0"]) {
NSLog (@ "succeeded ");
} Else
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "failed to get the ad ID" message: info delegate: selfcancelButtonTitle: @ "OK" otherButtonTitles: nil, nil];
[Alert show];
}
}