NSURLRequest to NSString

前端 未结 5 1843
渐次进展
渐次进展 2021-02-19 01:03

how can i convert NSURLRequest to NSString ?

相关标签:
5条回答
  • 2021-02-19 01:29

    In iOS 9 / Xcode 7 you can use request.URLString in Swift (or in Objective-C, actually)

    0 讨论(0)
  • 2021-02-19 01:37

    Depends on the information you want in the string. Do you want to have it contain all the values of the instance variables in the object? If so, you're going to need to write your own method that does that. Perhaps subclass NSURLRequest and override description. You could also use reflection to get at all the private ivars and print everything out from another class.

    Or just use a debugger to inspect the values.

    0 讨论(0)
  • 2021-02-19 01:37
    NSURLRequest *urlRequest;
    ...
    NSLog(@"%@", [urlRequest allHTTPHeaderFields]);
    NSLog(@"%@",[urlRequest valueForHTTPHeaderField:field]);
    

    In this example, urlRequest is an NSURLrequest (from the previous example above). The second line allows you to print the value of a specific field 'field' that has been added to the NSURLRequest.

    From Apple docs

    - (NSString *)valueForHTTPHeaderField:(NSString *)field Parameters
    

    field

    The name of the header field whose value is to be returned. In keeping with the HTTP RFC, HTTP header field names are case-insensitive.

    Return Value

    The value associated with the header field field, or nil if there is no corresponding header field.

    0 讨论(0)
  • 2021-02-19 01:39
    - (NSString*) urlRequestToString:(NSURLRequest*)urlRequest
    {
      NSString *requestPath = [[urlRequest URL] absoluteString];
      return requestPath;
    }
    
    0 讨论(0)
  • 2021-02-19 01:43

    If you're using swift, you can implement the following extension:

    extension NSURLRequest: DebugPrintable {
        public func extendedDescription() -> String {
    
            var result = "<\(NSStringFromClass(self.dynamicType)): " + String(format: "%p", self)
            result += "; HTTPMethod=" + (HTTPMethod ?? "nil") + "; URL=\(URL); timeoutInterval=" + String(format: "%.1fs", timeoutInterval) + "> {"
    
            // Add header fields.
            if let headers = allHTTPHeaderFields {
                result += "\nallHTTPHeaderFields {"
                for (key, value) in headers {
                    result += "\n\t\(key) : '\(value)'"
                }
                result += "\n}"
            }
    
            if let body = HTTPBody {
                result += "\nHTTPBody {\n " + ((NSString(data: body, encoding: NSASCIIStringEncoding) ?? "") as String) + "}"
            }
    
            return result + "\n}"
        }
    
        public override var debugDescription: String {
            return extendedDescription()
        }
    
        public override var description: String {
            return extendedDescription()
        }
    }
    

    Or check out this gist: https://gist.github.com/tomaskraina/d644af49e4968de85e34

    If printed out, it displays something similar to the following:

    <NSMutableURLRequest: 0x7fa91977e390; HTTPMethod=POST; URL=https://myapi.com/api/something; timeoutInterval=300.0s> {
    allHTTPHeaderFields {
        Accept-Encoding : 'gzip'
        Content-Type : 'application/json'
        Accept : 'application/json'
    }
    HTTPBody {
        {"key1":"value1"}
    }
    }
    
    0 讨论(0)
提交回复
热议问题