首先,我查看了如何发出发布请求,并阅读了多个线程和有关如何创建一个的文档,但是我的数据似乎不起作用。我有两个字段,说 x 和 html,我想对 callname 进行调用。GET 形式是 www.someserver.com/callname?x=something&y=something 到目前为止,我的 POST 代码如下所示:
NSString *baseURLString = @"http://www.someserver.com/callname"
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[[NSURL URLWithString:baseURLString] standardizedURL]];
NSString *fields = [NSString stringWIthFormat:@"x=%@&html=%@",x,htmlSource];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[NSData dataWithBytes:[fields UTF8String] length:strlen([fields UTF8String])]];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
NSURLConnection 委托方法是这样实现的
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSLog(@"Data Received");
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"Error: %@" , error);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"Connection Finished");
}
我错过了什么?除了编码选择之外,我的代码看起来与我发现的大多数示例完全相同。我将 html 源代码作为 y 的值传递是否重要?任何提示或提示将不胜感激。一般来说,我对 iOS 和 html 处理非常陌生,所以请原谅我对这个主题缺乏了解。谢谢你的时间!