在ios中使用POST请求

时间:2014-08-26 10:21:56

标签: ios asihttprequest nsurlrequest

我想以下列方式发布请求。

POST /oauth/token HTTP/1.1 
Host: sandbox.mydigipass.com 
Content-Type: application/x-www-form-urlencoded 
Accept: */*  

code=5bx5vq39nr35ctx954im1g314& 
client_id={CLIENT_ID}& 
client_secret={CLIENT_SECRET}& 
redirect_uri=https://your-site.com/callback& 
grant_type=authorization_code

是否可以在没有ASIHTTPRequest的情况下进行,请回复。

1 个答案:

答案 0 :(得分:0)

你尝试过NSURLConnection吗?有关详细信息,请参阅以下示例代码段(同步NSURLConnection请求)。

NSString *mRequestStr = WEBSERVICEURL;
NSURL *url1 = [NSURL URLWithString:mRequestStr];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url1
                                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                      timeoutInterval:60.0];
//[theRequest valueForHTTPHeaderField:body];
[theRequest setValue:mAppDelegate.mResponseMethods_.sessionToken forHTTPHeaderField:@"SessionToken"];
[theRequest setValue:mAppDelegate.mResponseMethods_.authToken forHTTPHeaderField:@"AuthToken"];
[theRequest setHTTPMethod:@"POST"];

NSHTTPURLResponse *response;
NSError *error;
NSData *responseData = [NSURLConnection
                        sendSynchronousRequest:theRequest
                        returningResponse:&response error:&error];
NSString *json_string = [[NSString alloc]
                         initWithData:responseData encoding:NSUTF8StringEncoding];
int statusCode = [((NSHTTPURLResponse *)response) statusCode];
DLog(@"Fit Bit connect(Re) %@ : statusCode: %d", json_string, statusCode);