send GET http request from iphone to local web server

旧城冷巷雨未停 提交于 2019-12-25 00:05:47

问题


Hi im working on a application that gives you information about your current location(Country, town,street) so I want to send the town and street to a local web server ! i found a code that technically works when i use this URL :

@"http://localhost:8888/jml/save.php?town=California&street=whatever"

But what i want is to get the values from a labels (townText/ streetText) and at this point nothing seems to happing ?!!

this is the code :

if (![self.townText.text isEqualToString:@""] && ![self.streetText.text isEqualToString:@""]) {
    NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat**:@"http://localhost:8888/jml/save.php?town=%@&street=%@",self.townText.text,streetText.txt**]];

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

    NSData *urlData;
    NSURLResponse *response;
    urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];

So please i need your help :) and thank you !


回答1:


Your iPhone isn't running the webserver itself - it's running on your development machine. Try switching "localhost" for your machine's IP address, and make sure that it's reachable from whatever network your development phone is on.

In addition - make sure that you're escaping the fields you're putting in the URL. If they contain characters that are not valid in a URL, your request will fail. You can escape your URL like so:

NSString* myURLString = [NSString
                         stringWithFormat:@"http://localhost:8888/jml/save.php?town=%@&street=%@", self.townText.text, self.streetText.text];
myURLString = [myURLString   
               stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Also: I'm assuming that those double-asterisks in your code sample are for illustration only, as that's not valid Obj-C syntax. You're also referencing a member (streetText.txt) that doesn't exist - you may have mistyped streetText.text.



来源:https://stackoverflow.com/questions/9930604/send-get-http-request-from-iphone-to-local-web-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!