How to send GEt request to PHP in iOS

前端 未结 1 1200
囚心锁ツ
囚心锁ツ 2021-02-04 22:22

Hi i have problem in sending GET request to a PHP, same PHP works fine when running it in web browser here are the code snippet of both the PHP and Obj-C PHP

$va         


        
相关标签:
1条回答
  • 2021-02-04 23:27

    The problem is that you set HTTPBody (by calling setHTTPBody on your request object) whilst GET-requests doesn't have a body, the passed data should be appended to the url instead. So to mimic the request your did in your browser it would simply be like this.

    NSString *url =[NSString stringWithFormat:@"http://sample.com/sample.php?value1=hi&value2=welcome"];
    NSLog(@"%@",url);
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [req setHTTPMethod:@"GET"]; // This might be redundant, I'm pretty sure GET is the default value
    NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:req delegate:self]autorelease];
    [connection start];
    

    You should of course make sure to properly encode the values of your querystring (see http://madebymany.com/blog/url-encoding-an-nsstring-on-ios for an example) to make sure that your request is valid.

    0 讨论(0)
提交回复
热议问题