当我上传数据时,put 请求不起作用?
Posted
技术标签:
【中文标题】当我上传数据时,put 请求不起作用?【英文标题】:put request is not working when i upload the data? 【发布时间】:2013-12-04 13:44:42 【问题描述】:我有“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 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);
请告诉我我的代码有什么问题......
【问题讨论】:
这里的方法不影响服务器端的数据,我没有得到响应 可以打印 url 和 putData 吗? 比写答案并接受它,所以它可能会帮助别人。 请提供我的代码???什么意思? 接受我的代码并投票,因为它可以工作......并且在服务器端生效...... 【参考方案1】:此代码完美运行。为 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];
【讨论】:
以上是关于当我上传数据时,put 请求不起作用?的主要内容,如果未能解决你的问题,请参考以下文章
使用 jQuery .ajax() PUT 请求 API 不起作用
QNetworkAccessManager HTTP Put 请求不起作用