我正在开发一个带有 REST 完整 Web 服务的应用程序。现在在这种情况下我遇到了一些问题,即使用 put 方法将字符串发送到服务器。我正在使用以下代码。
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://176.31.113.164/app/profil/add"]];
[request setValue:@"Digest adf@email.com:test" forHTTPHeaderField:@"Authorization"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];
//[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSDictionary *dic=[[NSDictionary alloc]init ];
dic=[NSDictionary dictionaryWithObjectsAndKeys:@"15",@"@idUsers",@"17",@"@idProfils",@"ddduu",@"address",@"ddd",@"city",@"ddd",@"country",@"adf@email.com <mailto:adf@email.com>",@"email",@"1",@"enabled",@"fff55",@"firstName",@"fffdd3",@"lastName",@"fffffcc",@"nickName",@"test",@"password",@"0",@"phone",@"0",@"postCode",@"ROLE_USER",@"role", nil];
NSString *jsonString = [jsonWriter stringWithObject:dic];
NSData *postData= [jsonString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
[request setHTTPBody:postData];
[request setHTTPMethod:@"PUT"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
NSURLConnection* _loginConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (_loginConnection==NULL)
{
NSLog(@"Problem");
}
[_loginConnection start];
接收到的数据是这样处理的:
NSString* tempString =[NSJSONSerialization
JSONObjectWithData:_receivedData
options:kNilOptions
error:&error];