简体   繁体   English

无法在 Swift iOS 中将字符串转换为 URL

[英]Unable to convert string to URL in Swift iOS

In order to make HTTP request call using URLSession,I need to convert string to URL first so I have tried everything but just can't convert this piece of string to URL(string: ).为了使用 URLSession 进行 HTTP 请求调用,我需要先将字符串转换为 URL,所以我尝试了所有方法,但就是无法将这段字符串转换为 URL(字符串:)。 This is the string: “http://api-aws-eu-qa-1.abc-cde.com/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99”这是字符串:“http://api-aws-eu-qa-1.abc-cde.com/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99”

I can't share the exact Url but format is all same.我不能分享确切的 Url 但格式都是一样的。 The actual url with same format works fine in browser but no way in Swift, also that works fine in POSTMAN I have also tried URLComponents which hepls me create the URL from components to URL but that fails with 400 status code response error. The actual url with same format works fine in browser but no way in Swift, also that works fine in POSTMAN I have also tried URLComponents which hepls me create the URL from components to URL but that fails with 400 status code response error. I really appreciate the help, I am completely bogged down with this isuue and can't proceed with my assignment.我非常感谢您的帮助,我完全陷入了这个问题,无法继续我的任务。

Update: Tested with fiddler and request was not going through with this URL - "“http://api-aws-eu-qa-1.abc-cde.com%E2%80%8B/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99”"更新:用提琴手测试,请求没有通过这个 URL -“”http://api-aws-eu-qa-1.abc-cde.com%E2%80%8B/v1/car-types/manufacturer ?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99”"

But when removed this %E2%80%8B in fiddler and resend it worked.但是,当在提琴手中删除此 %E2%80%8B 并重新发送时,它起作用了。 Any thoughts..?有什么想法吗..?

String to URL字符串到 URL

let url = URL(string: "the url string goes here")

URL to String URL 转字符串

let url = URL(string: "the url string goes here")
let urlString = url.absoluteString

Creating URL from URLComponents从 URLComponents 创建 URL

var url: URL? {
    var components = URLComponents()
    components.scheme = "https"
    components.host = "domain goes here" //example: twitter.com
    components.path = "/path/goes/here"
    components.queryItems = [
        URLQueryItem(name: "item1", value: string1),
        URLQueryItem(name: "item2", value: string2)
    ]

    return components.url
}

try to add HEADER to you request:尝试将 HEADER 添加到您的请求中:

let url = URL(string : urlString)
var request = URLRequest(url : url)
request.setValue("Application/json", forHTTPHeaderField : "Content-Type")

Your URL is:您的 URL 是:

http://api-aws-eu-qa-1.abc-cde.com%E2%80%8B/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99`

The "host" portion is: “主机”部分是:

api-aws-eu-qa-1.abc-cde.com%E2%80%8B

%E2%80%8B encodes a ZERO WIDTH SPACE . %E2%80%8B编码一个零宽度空间 This is a perfectly valid URL:这是一个完全有效的 URL:

URL(string: "http://api-aws-eu-qa-1.abc-cde.com%E2%80%8B/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99")
=> Optional(http://api-aws-eu-qa-1.abc-cde.com%E2%80%8B/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99)

However, there is no such host as api-aws-eu-qa-1.abc-cde.com%E2%80%8B , so you should expect the actual connection to fail.但是,没有像api-aws-eu-qa-1.abc-cde.com%E2%80%8B这样的主机,因此您应该预计实际连接会失败。

I expect that whatever is generating your URL strings has a bug.我希望生成 URL 字符串的任何东西都有一个错误。

If you are having trouble creating the URL itself (if URL(string:) return nil, then I expect this is not your actual URL string.如果您在创建 URL 本身时遇到问题(如果URL(string:)返回 nil,那么我预计这不是您的实际 URL 字符串。

If you construct an URLComponents, the results are correct but may be slightly misleading:如果你构造一个 URLComponents,结果是正确的,但可能会有点误导:

URLComponents(string: "http://api-aws-eu-qa-1.abc-cde.com%E2%80%8B/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99")?.host
Optional("api-aws-eu-qa-1.abc-cde.com")

While this looks like "api-aws-eu-qa-1.abc-cde.com" , the string actually has a ZERO WIDTH SPACE at the end, which is invisible (but still part of the host field, which will correctly fail if you try to connect to it).虽然这看起来像"api-aws-eu-qa-1.abc-cde.com" ,但该字符串实际上在末尾有一个零宽度空间,这是不可见的(但仍然是主机字段的一部分,它将正确地失败如果您尝试连接到它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM