OGeek|极客世界-中国程序员成长平台

标题: 发布数据时,IOS8 参数未附加到 URL [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 07:58
标题: 发布数据时,IOS8 参数未附加到 URL

我正在调用 REST 服务来发布 lat & lng,但是数据没有发送到服务器

-(void)locationManagerCLLocationManager *)manager didUpdateLocationsNSArray *)locations
{
    CLLocation *crnLoc = [locations lastObject];
    self.locationDesc.text = [NSString  stringWithFormat"%@",crnLoc.description];
    NSLog(@"%@",crnLoc.description);
    NSURL *aUrl = [NSURL URLWithString"http://localhost/web/location/create.php?"];
    NSMutableURLRequest *request = [NSMutableURLRequest
                                requestWithURL:aUrl];
    NSString *params = [[NSString alloc] initWithFormat"latitude=%g&longitude=%g", crnLoc.coordinate.latitude, crnLoc.coordinate.longitude];
   [request setHTTPMethod"OST"];
   [request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
   [[NSURLConnection alloc] initWithRequest:request delegate:self];
   NSLog(@"%@",request.URL);
}

我在上面的代码中做错了什么吗?我可以使用 REST Easy 客户端(firefox 插件)发布数据。



Best Answer-推荐答案


使用此代码

CLLocation *crnLoc = [locations lastObject];
self.locationDesc.text = [NSString  stringWithFormat"%@",crnLoc.description];
NSLog(@"%@",crnLoc.description);
NSURL *aUrl = [NSURL URLWithString"http://localhost/web/location/create.php?"];
NSMutableURLRequest *request = [NSMutableURLRequest
                            requestWithURL:aUrl];
NSString *params = [[NSString alloc] initWithFormat"latitude=%g&longitude=%g", crnLoc.coordinate.latitude, crnLoc.coordinate.longitude];
NSData *postData = [params dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat"%d",[postData length]];
[request setHTTPMethod"OST"];
[request setValue:postLength forHTTPHeaderField"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(con) 
{
   NSLog(@"Connection Successful");
} 
else 
{
   NSLog(@"Connection could not be made");
} 

供引用:

Sending an HTTP POST request on iOS

关于发布数据时,IOS8 参数未附加到 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30755081/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4