Unsupported URL in NSURLRequest

[亡魂溺海] 提交于 2019-12-20 09:28:33

问题


If I run this request from my terminal I can see the JSON requests as normally:

curl -XGET 192.168.0.6:8888/scripts/data/backend2/index.php/name/_all

My code for the NSURlRequest is this:

 NSURLRequest *request = [NSURLRequest requestWithURL:
                             [NSURL URLWithString:@"192.168.0.6:8888/scripts/data/backend2/index.php/name/_all"]];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];

And I am getting this error:

didFailWithError
2013-11-29 22:31:08.164 Ski Greece[607:a0b] Connection failed: Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0xcd042d0 {NSErrorFailingURLStringKey=192.168.0.6:8888/scripts/data/backend2/index.php/name/_all, NSErrorFailingURLKey=192.168.0.6:8888/scripts/data/backend2/index.php/name/_all, NSLocalizedDescription=unsupported URL, NSUnderlyingError=0xdbdcc70 "unsupported URL"}

How can I make the call to that URL? I cannot access the server code - I know it is just setup to return me what I need, if I call that URL?


回答1:


Try to include appropriate url scheme to your url, e.g.

[NSURL URLWithString:@"http://www...




回答2:


In my case I fixed it with this :

strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

strURL contains the string with the URL.




回答3:


In my case, I visit a service running on my own mac, so my url is 127.0.0.1:8080/list

After I add a http:// scheme. It works!

Now it is http://127.0.0.1:8080/list instead of 127.0.0.1:8080/list




回答4:


I too struggled for the same error, even though the url path was correct but there was a space in it, before http, like below:

NSString *path = @" http://www.mylink/";
NSURL *url = [NSURL URLWithString:path];

so I was getting url as nil and so it was giving "unsupported URL". then by removing the space worked for me.




回答5:


In my case spaces are added to my URL. I have removed spaces and run. Please make sure you have not added any spaces to your URL even when you are passing parameters. Hope it helps someone.




回答6:


It seems its a malformed URL or it's not a valid url at all, try to hit this url in browser, I think you will not get any result. error code=-1002 occurs when the url is unsupported.




回答7:


same answer as Alf G but with IOS 9+

strUrl = [strUrl stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLHostAllowedCharacterSet]



回答8:


Like stated before, a space in the URL can cause this but it's also possible that your string contains an unsupported character. For example, if you copy and paste a URL from a PDF, Word or other document it might contain unsupported characters. To the eye it looks fine but not the compiler.

To fix this, in your [NSURL URLWithString:@"http://blabhblabh"] method, delete the entire line of code, not just the url, and retype the link and method by hand.




回答9:


In my case, I was passing an unwrapped optional in Swift. Once I unwrapped the optional to string, the URL was accepted correctly.



来源:https://stackoverflow.com/questions/20294328/unsupported-url-in-nsurlrequest

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