在iOS上发送带有标题表单数据请求的HTTP POST

时间:2016-02-22 10:21:29

标签: ios objective-c http-post

我正在调用带有HTTP头文件的odata post api是“form-data”。以下是我的代码: -

NSURL *restURL = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:restURL];
    [request setHTTPMethod: getorpost];
    if (jsonData != nil) {
                [request setValue:@"application/form-data" forHTTPHeaderField:@"Content-Type"];

         [request setHTTPBody:jsonData];
    }

    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    if (connection) {
        responseData = [[NSMutableData alloc] init];
    } 

我的回答低于: -

处理HTTP请求导致异常。有关详细信息,请参阅此异常的“响应”属性返回的HTTP响应

但是,它在Postman中运作良好。任何人都可以建议我的代码中的错误在哪里。

谢谢,

2 个答案:

答案 0 :(得分:0)

使用此

NSURL  *url = [NSURL URLWithString:url_str];
NSLog(@"%@",datastring);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSMutableData *requestBody = [[NSMutableData alloc] initWithData:[datastring dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"no-cache" forHTTPHeaderField:@"Cache-Control"];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[requestBody length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestBody];
httpResponse=[[NSHTTPURLResponse alloc]init];
receivedData=[[NSMutableData alloc]init];
connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

if(connection)
{
    NSLog(@"%@ calling with datastring: %@", url, datastring);
}

<强>代表

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 {

     httpResponse = (NSHTTPURLResponse *) response;
     NSLog(@"%d", httpResponse.statusCode);
      NSLog(@"%@",[httpResponse description]);

}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
 }

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
receivedData = [[NSMutableData alloc]init];
httpResponse=[[NSHTTPURLResponse alloc]init];
NSLog(@"%@",[NSString stringWithFormat:@"Connection failed: %@", [error description]]);
}

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error;


NSString *retVal = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"retVal=%@",retVal);
}

答案 1 :(得分:0)

-(void)ViewDidLoad
{
NSMutableDictionary *postData = [[NSMutableDictionary alloc]init];
[postData setObject:uid forKey:@"id"];
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:postData options:kNilOptions error:nil];
NSString *jsonInputString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *post = [[NSString alloc]initWithFormat:@"%@",jsonInputString];

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"YOUR URL "]];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:120.0];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSError *error;
NSURLResponse *response;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSDictionary *jsonDict;
if (responseData != nil)
{
    jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    NSLog(@"jsonDoct == %@",jsonDict);
}
else
{
    NSLog(@"RESONPSE IS NULL");
}

if (error)
    {
        NSLog(@"error %@",error.description);
    }
    }