The configuration of the afnetworking request header is used to complete the authentication of HTTP Basic Auth

Source: Internet
Author: User
Tags auth

set Request headers before afnetworking 3.0

Afhttprequestoperationmanager *manager = [Afhttprequestoperationmanager manager];

[Manager.securitypolicy = [Afsecuritypolicy policywithpinningmode:afsslpinningmodenone];

[Manager.requestserializer = [Afjsonrequestserializer serializer];

[Manager.responseserializer = [Afjsonresponseserializer serializer];

[Manager.requestserializer setvalue:@ "Application/json" forhttpheaderfield:@ "Accept"];

[Manager.requestserializer setvalue:@ "Application/json charset=utf-8" forhttpheaderfield:@ "Content-Type"];



afnetworking 3.0 to set the request header, in more detail, post and get are available

-(afhttpsessionmanager*) Defaulthttpmanager {//Initialize object Afhttpsessionmanager *manager = [Afhttpsessionmanager Manage
    R];
    Returns the format JSON Manager.responseserializer = [Afjsonresponseserializer serializer]; Acceptable text parameter Specification Manager.responseSerializer.acceptableContentTypes = [Nsset setwithobjects:@ "Application/json", @ "text/
    
    HTML ", @" Text/json ", @" Text/javascript ", nil];
    Start setting up request headers [Manager.requestserializer setvalue:@ "Application/json" forhttpheaderfield:@ "Content-type"];
    [Manager.requestserializer setvalue:@ "fill in the system version" forhttpheaderfield:@ "Sysversion"];
    [Manager.requestserializer setvalue:@ "fill in the equipment" forhttpheaderfield:@ "Devicemodel"];
    
    [Manager.requestserializer setvalue:@ "fill in app version" forhttpheaderfield:@ "AppVersion"];
    UID is the corresponding ID of each user ciphertext is the password NSString * part1 = [NSString stringwithformat:@ "%lld:%@", Uid,ciphertext]; NSString * authorization = [NSString stringwithformat:@ "Basic%@" by means of the format of ID: password, in basic mode, [Part1 Base64enCode]]; Set the authorization method to set the header [Manager.requestserializer setvalue:authorization forhttpheaderfield:@ Authorization
    "]; or call the AF method directly to set//-(void) Setauthorizationheaderfieldwithusername: (NSString *) Username//Password: (nsstring *) password//{//NSData *basicauthcredentials = [[NSString stringwithformat:@ '%@:%@ ', username, password] Data
Usingencoding:nsutf8stringencoding]; NSString *base64authcredentials = [basicauthcredentials base64encodedstringwithoptions: (NSDataBase64EncodingOpti
ONS) 0]; [Self setvalue:[nsstring stringwithformat:@ "Basic%@", Base64authcredentials] forhttpheaderfield:@ "Authorization"
"];
} return manager; }

Take a look at the example

{

Accept = "*/*";

"Accept-encoding" = "gzip, deflate";

"Accept-language" = "en;q=1";

appversion = "2.1.0";

Authorization = "Basic Ode2ndy6zg15t29udxdqv3h5q3myuvfss2fgy1nmn0lpclnnt0zntxg2b2hkve5vnutrnelnt1fhohg0zg1xdlvxqkr3qnrtnurll2h4wmjty0rewmowl05pb Mluq1azbmtezujkte82qxv5a3nqd2jna1hzeetxctrivukrbhr0qi8xeghiyw5unwjmuk0xzgpnumtpwvrjsljomwhhskqzae54b3jsvfzcd2jsbnlrpq ==";

Connection = "Keep-alive";

"Content-length" = 452;

"Content-type" = "application/x-www-form-urlencoded";

Cookie = "TWT__SESSION=DEDK2QI0PRMILL5OKQU6HKDJC0; TWT__NICK_NAME=IOIIII; twt__user_name=13826241102; twt__userid=81646; twt__user_login= 26dhly-obwnrmwdra5dxqmbtwqpdz5yoj41kmzlimgmaajrnzirwx6xgpkon0tszv76obcjemwzkl5mdxjssmzlwxm1kxmwtzsawz2avxjmzlpnwny-rmz _HKCOMYZZUM2EYA2UWZ2BNMGPK ";

Devicemodel = "x86_64";

Host = "u.taowaitao.cn";

"If-modified-since" = "Wed, SEP 2016 09:37:40 GMT";

Sysversion = "ios10.0";

"User-agent" = "taowaitao/2.1.0" (IPhone; IOS 10.0; scale/2.00) ";

}




Note that the head is set, and the parameters on the URL can be passed to get or post for the parameter method.


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.