URL structure of Request Response in IOS Network

Source: Internet
Author: User
Tags ftp protocol

For IOS developers, the most important thing is to access network resources. Data is provided to us by servers. We generally access our network resources through APIS. Generally, a resource may correspond to many URLs, but a URL only corresponds to one resource and cannot correspond to many resources. But there are also some exceptions, such as when the hostName references a fuzzy host. In the IOS system, the NSURL object is used to manage the URL object. A URL is usually composed of the following parts: Image protocol: This protocol component specifies the application layer protocol to communicate with the server. You may know that there are other protocols, such as the ftp protocol and http protocol. The dominant position of the http protocol is challenged by the pre-http protocol. Another commonly used protocol is the file protocol. The file protocol is the protocol that IOS apps use to retrieve data in the sandbox. If you use NSURL but do not use other protocols, this protocol is used by default. Credentials: some http servers support the URL of user creden。, which is a challenge for basic authentication. As shown in, this part of the Credential contains the authentication of the user name and password. This format is not very common and is considered to be less secure than other authentication methods. Hostname: this component contains the TCP hostname and IP address about the host containing the resources it wants. If the URL protocol is file, you should omit these items. A URL is broken for an independent resource. This is an exception when the relative and local host names are used. For example, if your host name is hostname, the URL is associated with the local machine and the same URL can be used to associate different resources on different machines. Port: This URL port specifies the Port that the client should connect. If it is ignored, the client will use the default port for this protocol, such as http port 80 and https port 443. The best practice is to use these port values when the application runs on the internet because some network proxies and firewalls will block non-standard port values for security or privacy reasons. Absolute-path: this Absolute path specifies the path of network resources. If the http server has a directory tree. This absolute path may include any number of Path Components. Each component is separated by a "/" character. An absolute path may not contain a question mark, space, carriage return, or line break. Some Rest services use the path part as a unique means to identify an object stored in the database. For example, a path/customer/456/address/0 will specify the address where the identifier 456 is indexed at 0. Query: the last part of the URl is the Query string. Is this value used from the absolute path?. Multiple parameters are separated. The query string cannot contain line breaks with Carriage Return spaces. This is because there are limits on the content of the query string that decides the path and splicing. URLs are usually encoded as percentages. Rfc 3986 specifies the URL percentage encoding details. IOS provides a String object method to perform URL encoding. The following code snippet shows how to encode the code. NSString * urlString = @ "http://myhost.com? Query = This is a question "; NSString * encoded = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; the result of This encoding is: http://myhost.com? Query = This % 20is % 20a % 20question. Each space is replaced with the % 20 sequence. The difference between this encoding and URL encoding is that it is not encoded & character. In this way, the URL parameters are intact. URL encoding will encode & symbols, question marks, and other punctuation marks. If your query string contains these strings. You need to implement a more thorough encoding method. The purpose is to convert these characters to % + ASCII instead. You can remove these symbols. @ Implementation NSURL (mm) + (NSURL *) URLWithBaseString :( NSString *) baseString parameters :( NSDictionary *) parameters {NSMutableString * urlString = [NSMutableString string]; // The URL starts with the base string [urlString appendString: baseString]; [urlString appendString: baseString]; NSString * escapedString; NSInteger keyIndex = 0; for (id key in parameters) {// First Parameter needs to be prefixed w Ith? And any other parameter needs to be prefixed with an & if (keyIndex = 0) {escapedString = (NSString *) values (kCFAllocatorDefault, (CFStringRef) [parameters valueForKey: key], NULL, CFSTR (":/? # [] @! $ & '() * +,; = "), KCFStringEncodingUTF8); [urlString appendFormat :@"? % = % @ ", Key, escapedString]; [escapedString release];} else {escapedString = (NSString *) values (kCFAllocatorDefault, (CFStringRef) [parameters valueForKey: key], NULL, CFSTR (":/? # [] @! $ & '() * +,; = "), KCFStringEncodingUTF8); [urlString appendFormat: @" & %@=%@ ", key, escapedString]; [escapedString release];} keyIndex ++;} return [NSURL URLWithString: urlString];} @ end copy code example: NSString * baseString = @ "http://twitter.com/statuses/update.xml "; NSDictionary * dictionary = [NSDictionary dictionaryWithObjectsAndKeys: @ "This is my status", @ "status", @ "meng ya", @ "meyers", nil]; NSURL * url = [NSURL URLWithBaseString: baseString parameters: dictionary]; NSLog (@ "the url: % @", url );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.