About the bug in afnetworking header

Source: Internet
Author: User


About the bug in afnetworking header

[abstract: afnetworking that is in the opening of the iOS Contempoly road, a search on the internet a big push, but the detailed usage I will have no way, by chance I will rectify some of the detailed usage. The first joint of the day I'm applying some bugs that are close to the Header.

Afnetworking this in the development of iOS not much to say, a search on the internet a big push, but the specific usage I will not say, there is time I will tidy up some specific usage. Today I'm going to focus on some of the bug issues that I'm using on the Header.
First of all about a login to keep, I and the server personnel through the specific development communication, decided to use token as the only sign, that is, every time you log in, the server will return to you a token, and then I can save this token, Add tokens by setting header headers in exchange for some of the data I Need.
It's easy to say and start doing.
first, I need to get the data through token on a report function page, so I add the token code to the header head as follows

    Nsuserdefaults *defaults=[Nsuserdefaults standarduserdefaults];NSString *token=[defaults objectforkey:@"token"];NSLog (@"%@", token);Nsmutabledictionary *parameters=[Nsmutabledictionary dictionary]; Afhttprequestoperationmanager*mager=[afhttprequestoperationmanager manager];[mager.requestserializer setvalue:@ "application/json" forhttpheaderfield:@ "content-type"];Add token information to the head [mager. Requestserializer Setvalue:token forhttpheaderfield:@"token"]; Mager. Responseserializer = [afjsonresponseserializer serializer]; mager. requestserializer=[ Afjsonrequestserializer serializer]; NSLog (@"%@", mager. Requestserializer. httprequestheaders); [mager get:@"http://192.168.0.203:5000/report/1?m=d" parameters:parameters success:^ (afhttprequestoperation * operation, ID Responseobject) { NSLog (@"%@", responseobject);} failure:^ (afhttprequestoperation * operation, nserror *error) { NSLog (@"request failed");}];          

Here are some of the settings below.
1.

setValue:@"" forHTTPHeaderField:@""];

This method is to forget the header to add Key_value key value pairs, that is, to add information to the head, this can be based on server requirements to join the Header. There are many people in the HTTP request (notice the security difference between HTTPS and http), often the request data needs to be placed in the AFN body, in fact, so far not put in the header inside Security. Let me cite two examples:
first, this is putting tokens in the Body.

    Nsuserdefaults *defaults=[Nsuserdefaults standarduserdefaults];NSString *token=[defaults objectforkey:@"token"];Nsmutabledictionary *parameters=[nsmutabledictionary dictionary]; //token put in Parameters parameters[@"token"]=token; Afhttprequestoperationmanager*mager=[afhttprequestoperationmanager manager]; Mager. Responseserializer = [afjsonresponseserializer serializer]; mager. requestserializer=[ Afjsonrequestserializer serializer]; [mager get:@"http://192.168.0.203:5000/report/1?m=d" parameters:parameters success:^ (afhttprequestoperation * operation, ID Responseobject) { NSLog (@"%@", responseobject);} failure:^ (afhttprequestoperation * operation, nserror *error) { NSLog (@"request failed");}];           

This is putting tokens in the Header.

    Nsuserdefaults *defaults=[Nsuserdefaults standarduserdefaults];NSString *token=[defaults objectforkey:@"token"];Nsmutabledictionary *parameters=[Nsmutabledictionary dictionary]; Afhttprequestoperationmanager*mager=[afhttprequestoperationmanager manager];Tokens are placed in the header [mager. Requestserializer Setvalue:token forhttpheaderfield:@ "token"; //print The information in the header nslog (@ "%@", Mager.requestserializer.responseserializer = [afjsonresponseserializer serializer]; mager.requestserializer=[afjsonrequestserializer serializer]; [mager Get:@ "http://192.168.0.203:5000/report/1?m=d" parameters:parameters success:^ ( Afhttprequestoperation *operation, id Responseobject) { NSLog (@ "%@", responseobject);} failure:^ (afhttprequestoperation *operation, nserror *error) {nslog (@" request failed ");}];   

Here are a few codes to add to the understanding

This is to tell afn, I accept the server came from a josn data, and then AFN will automatically turn us into a dictionary, is not very convenient? Mager.responseserializer = [afjsonresponseserializer serializer] ;//declare that the requested data is JSON type, if this is stated, then our incoming dictionary parameters will automatically be AFN converted to JOSN type to the server Mager.requestserializer=[afjsonrequestserializer serializer];// The following sentence means to tell AFN don't parse, I give what data is what data mager.requestserializer=[ Afhttprequestserializer Serializer];//the following sentence means to tell AFN don't parse, just give me bare data can mager.responseserializer = [afhttpresponseserializer serializer];// This sentence tells afn, if the server returned data did not find the type I told, such as josn, then give me text alive HTML type Mager.responseserializer< Span class= "hljs-preprocessor" >.acceptablecontenttypes = [nsset setwithobject:@ "text/ HTML "]           

next, we use header to package token to the server to get the data, the code is as follows

    Nsuserdefaults *defaults=[Nsuserdefaults standarduserdefaults];NSString *token=[defaults objectforkey:@"token"];Nsmutabledictionary *parameters=[Nsmutabledictionary dictionary]; Afhttprequestoperationmanager*mager=[afhttprequestoperationmanager manager]; [mager. Requestserializer Setvalue:token forhttpheaderfield:@ "token"; Mager.responseserializer = [afjsonresponseserializer serializer]; mager.requestserializer=[afjsonrequestserializer serializer]; nslog (@ "%@", mager "http://192.168.0.203:5000/report/1?m=d" parameters:parameters success:^ ( Afhttprequestoperation *operation, id Responseobject) { NSLog (@ "%@", responseobject);} failure:^ (afhttprequestoperation *operation, nserror *error) {nslog (@" request failed ");}];   

As a result, the server can not accept tokens inside the header, what's going on? So I all kinds of debugging, and finally found the Problem. As long as the hidden Mager.requestserializer=[afjsonrequestserializer serializer], this line, since not in the body to pass any parameters, the first note this sentence, the result is ok! So the problem is lifted? No! Because the next day found the problem!

    Nsuserdefaults *defaults=[Nsuserdefaults standarduserdefaults];NSString *token=[defaults objectforkey:@"token"];Nsmutabledictionary *parameters=[Nsmutabledictionary dictionary]; parameters[@"logout"][email protected]"1"; Afhttprequestoperationmanager*mager=[afhttprequestoperationmanager manager]; [mager. Requestserializer Setvalue:token forhttpheaderfield:@ "token"; Mager.responseserializer = [afjsonresponseserializer serializer]; //mager.requestserializer=[afjsonrequestserializer serializer]; nslog (@ "%@", mager "http://192.168.0.203:5000/report/1?m=d" parameters:parameters success:^ ( Afhttprequestoperation *operation, id Responseobject) { NSLog (@ "%@", responseobject);} failure:^ (afhttprequestoperation *operation, nserror *error) {nslog (@" request failed ");}];   

At this point, the server needs a header token, and a parameter logout, and I commented Mager.requestserializer=[afjsonrequestserializer serializer]; The server simply receives the parameter is not the JSON type, alas, this can do! Open this comment, can accept the JOSN parameter but can't find the header of the token, do not open it, Find header head heavy token but the parameter parameters inside the type is not josn! Then try yourself into a json-type string, and don't let AFN go! As a result I had a jsonkit to turn, the result is still not, the server accept is not josn data, not JSON data, then do not return data! This is a mess for me! Set header header and Mager.requestserializer=[afjsonrequestserializer serializer]; this code conflict? No way! So I started all kinds of baidu, forum, blog, however, China's ability to reproduce really have to call a cow, Stereotyped. Even the font is not changed, can not find the Way. So the server staff also help find, he went to a foreign blog above, looked at half a day, suddenly told me, you change the order to try? Order? No way, It's a Joke. Did he say you tried? anyway, there is no recruit, try to try the bai!

    Nsuserdefaults *defaults=[Nsuserdefaults standarduserdefaults];NSString *token=[defaults objectforkey:@"token"];Nsmutabledictionary *parameters=[Nsmutabledictionary dictionary]; parameters[@"logout"][email protected]"1"; Afhttprequestoperationmanager*mager=[afhttprequestoperationmanager manager];Two lines of Mager are written First.. Responseserializer = [afjsonresponseserializer serializer]; Mager. Requestserializer=[afjsonrequestserializer serializer];//in setting header [mager.requestserializer Setvalue:token Forhttpheaderfield:@ "token"; //print The header information nslog (@ "%@", Mager.requestserializer "http://192.168.0.203:5000/report/1?m=d" parameters:parameters success:^ ( Afhttprequestoperation *operation, id Responseobject) { NSLog (@ "%@", responseobject);} failure:^ (afhttprequestoperation *operation, nserror *error) {nslog (@" request failed ");}];   

result, run! Ok! No problem, josn accepted as usual, header head also received as Usual. At this time, heart 10,000 "grass Mud horse" rushed over, this is to tease me! So I tried it a few times in a different order, eh! Really should first put mager.responseserializer = [afjsonresponseserializer serializer]; mager.requestserializer=[ Afjsonrequestserializer serializer]; These two lines of code are written in front of the header of the set header! Hey! The problem finally solved, here I put that foreign blog site, inside a large pile of english, I am not good, really do not understand!
Https://www.parse.com/questions/using-afnetworking-with-parse-rest-api-for-post
well, after the recording of this bug, can finally write a good code!

About the bug in afnetworking header

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.