When iOS POST requests contain Chinese Characters

Source: Internet
Author: User
Tags unsupported

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];

}

}





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.