Iphone 上传数据时put请求不起作用?
我有“PUT”方法的代码,请告诉我整个方法有问题…代码不影响服务器端…请告诉我,因为我比较新鲜,所以我不知道我的代码有什么问题Iphone 上传数据时put请求不起作用?,iphone,ios7,xcode5,put,Iphone,Ios7,Xcode5,Put,我有“PUT”方法的代码,请告诉我整个方法有问题…代码不影响服务器端…请告诉我,因为我比较新鲜,所以我不知道我的代码有什么问题 -(void)PutMethod:(NSString *)url andPostData:(NSString *)putData { NSLog(@"%@",url);//here get the right url which i want.. NSLog(@"%@",putData);//here get right data which i w
-(void)PutMethod:(NSString *)url andPostData:(NSString *)putData
{
NSLog(@"%@",url);//here get the right url which i want..
NSLog(@"%@",putData);//here get right data which i want to upload...
NSData *putNSData=[putData dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSLog(@"NSData : %@",putNSData);
NSString *putLength =[NSString stringWithFormat:@"%lu",(unsigned long)[putData length]];
NSLog(@"%@",putLength);
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"PUT"];
[request setValue:putLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:putNSData];
NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
[connection start];
if (connection)
{
putNSData=[[NSMutableData alloc]init];
}
}
这是我的委托方法:
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *Success=[dict valueForKey:@"Success"];
NSString *AccessToken=[dict valueForKey:@"AccessToken"];
NSString *UserId=[dict valueForKey:@"UserId"];
NSLog(@"ReceiveData :Success : %@ \n AccessToken : %@ \n UserId : %@",Success,AccessToken,UserId);
}
请告诉我我的代码出了什么问题……这个代码工作得很好。为PUT执行此操作:
- (void)PutMethod:(NSString *)url andPostData:(NSString *)putData {
NSLog(@"%@", url);
NSLog(@"%@", putData);
NSData *putNSData = [putData dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSLog(@"NSData : %@",putNSData);
NSString *putLength = [NSString stringWithFormat:@"%lu", (unsigned long)[putData length]];
NSLog(@"%@", putLength);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"PUT"];
[request setValue:putLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:putNSData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
if (connection) {
putNSData = [[NSMutableData alloc] init];
}
}
这里的方法不影响服务器端的数据,我没有得到响应。你可以打印url和putData吗?然后写下答案并接受它,这样可能会帮助其他人。请更新我的代码???这意味着什么?接受我的代码并投票,因为它起作用了。。。。和服务器端的效果。。。