ios NSURLRequest 中不支持的 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20294328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:34:05  来源:igfitidea点击:

Unsupported URL in NSURLRequest

iosobjective-cjsonnsurlrequest

提问by ghostrider

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

如果我从我的终端运行这个请求,我可以像往常一样看到 JSON 请求:

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

My code for the NSURlRequest is this:

我的 NSURlRequest 代码是这样的:

 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?

如何调用该 URL?我无法访问服务器代码 - 我知道它只是设置为返回我需要的东西,如果我调用那个 URL?

回答by Vizllx

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

尝试在您的网址中包含适当的网址方案,例如

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

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

回答by Alf G

In my case I fixed it with this :

在我的情况下,我用这个修复了它:

strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

strURL contains the string with the URL.

strURL 包含带有 URL 的字符串。

回答by KANGBACH

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

就我而言,我访问了在我自己的 mac 上运行的服务,所以我的 url 是 127.0.0.1:8080/list

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

在我添加一个 http:// 方案之后。有用!

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

现在是http://127.0.0.1:8080/list而不是 127.0.0.1:8080/list

回答by keshav

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:

我也为同样的错误苦苦挣扎,即使 url 路径是正确的,但在 http 之前有一个空格,如下所示:

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.

所以我得到的 url 为 nil,所以它给出了“不受支持的 URL”。然后通过删除对我有用的空间。

回答by Aviv Frenkel

same answer as Alf G but with IOS 9+

与 Alf G 相同的答案,但使用 IOS 9+

strUrl = [strUrl stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLHostAllowedCharacterSet]

回答by Narasimha Nallamsetty

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.

在我的情况下,空格被添加到我的 URL 中。我已经删除了空格并运行。请确保即使在传递参数时也没有在 URL 中添加任何空格。希望它可以帮助某人。

回答by Sanchit Kumar Singh

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.

它似乎是一个格式错误的 URL 或者它根本不是一个有效的 url,尝试在浏览器中点击这个 url,我认为你不会得到任何结果。当 url 不受支持时,会发生错误代码=-1002。

回答by Justin Domnitz

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

就我而言,我在 Swift 中传递了一个未包装的可选项。一旦我解开可选的字符串,URL 就被正确接受了。

回答by ClintChil

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.

如前所述,URL 中的空格可能会导致这种情况,但您的字符串也可能包含不受支持的字符。例如,如果您从 PDF、Word 或其他文档复制和粘贴 URL,它可能包含不受支持的字符。眼睛看起来不错,但编译器不行。

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.

要解决此问题,请在您的[NSURL URLWithString:@"http://blabhblabh"]方法中删除整行代码,而不仅仅是 url,然后手动重新键入链接和方法。