Friday, September 28, 2012

HTTP GET & POST example


 1.  first http://open.t.sina.com.cn / developer in the application for, and then create a different application, obtain the corresponding App Key (the source link below is the app key)
 
2. login authentication:
NSString * authString = [ NSString stringWithFormat : "%@:%@" , sinaIDField . text , sinaPasswordField . text ];
NSData * authData = [authString dataUsingEncoding : NSUTF8StringEncoding ];
NSString * authValue = [ NSString stringWithFormat : @ "Basic% @" , [authData base64EncodingWithLineLength : 80 ]];

NSURL * url = [ NSURL URLWithString : @ " http://api.t.sina.com.cn/account/verify_credentials.xml?source=3930264715 " ];
NSMutableURLRequest * Request = [[ NSMutableURLRequest alloc ] initWithURL : url];
[Request setHTTPMethod : @ "GET" ];
[Request setValue : authValue forHTTPHeaderField : @ "Authorization" ];

NSURLResponse * response;
NSError * Error;

[ NSURLConnection sendSynchronousRequest : Request returningResponse : & response Error : & Error];

[Request Release ];

NSHTTPURLResponse * HttpResponse = ( NSHTTPURLResponse *) response;

int statusCode = [HttpResponse statusCode ];

NSLog ( @ "status code =% d" , statusCode);

if (statusCode! = 200 ) {
alertTitle = @ " username or password error " ;
alertMassage = @ " Please enter the correct username and password! " ;
} else {
alertTitle = @ "" ;
alertMassage = @ " login success! " ;
}
}
UIAlertView * Alert = [[ UIAlertView alloc ] initWithTitle : alertTitle
Message : alertMassage
delegate : nil cancelButtonTitle : @ " OK "
otherButtonTitles : nil ];
[Alert Show ];
[Alert Release ];

 
3. the content (image text) is sent to microblogging Sina
NSString * authString = [ NSString stringWithFormat : "%@:%@" , [[ NSUserDefaults standardUserDefaults ] objectForKey : @ "sinaID" ], [[ NSUserDefaults standardUserDefaults ] objectForKey : @ "sinaPassword" ]];
NSData * authData = [authString dataUsingEncoding : NSUTF8StringEncoding ];
NSString * authValue = [ NSString stringWithFormat : @ "Basic% @" , [authData base64EncodingWithLineLength : 80 ]];


NSString * boundary = @ "0xKhTmLbOuNdArYckkk" ;
NSString * filename = @ " test . jpg " ;
 
NSData * imageData = UIImageJPEGRepresentation (shareImage, 1 );


NSString * bodyPrefixString = [ NSString stringWithFormat : "--%\ r \ n " , boundary];
NSString * bodySuffixString = [ NSString stringWithFormat : @ "\ r \ n -% @ - \ r \ n" , boundary];
NSString * contentDisposition = [ NSString stringWithFormat : @ "Content-Disposition: form-Data; name = \" pic \ "; filename = \"% @ \ "\ r \ n" , filename];
NSString * contentImageType = [ NSString stringWithFormat : @ "Content-Type: image /% @ \ r \ n" , [filename pathExtension ]];
NSString * contentTransfer = @ "Content-Transfer-Encoding: binary \ r \ n \ r \ n" ;

NSString * bodyUpdateField = [ NSString stringWithFormat : @ "Content-Disposition: form-Data; name = \" status \ "\ r \ n \ r \ n% @ \ r \ n" , [ NSString stringWithFormat : "%@" , TextView . text ]];
NSMutableData * postBody = [ NSMutableData Data ];

[PostBody appendData : [bodyPrefixString dataUsingEncoding : NSUTF8StringEncoding allowLossyConversion : NO ]];
[PostBody appendData : [bodyUpdateField dataUsingEncoding : NSUTF8StringEncoding ]];
[PostBody appendData : [bodyPrefixString dataUsingEncoding : NSUTF8StringEncoding allowLossyConversion : NO ]];
[PostBody appendData : [contentDisposition dataUsingEncoding : NSUTF8StringEncoding ]];
[PostBody appendData : [contentImageType dataUsingEncoding : NSUTF8StringEncoding ]];
[PostBody appendData : [contentTransfer dataUsingEncoding : NSUTF8StringEncoding ]];
[PostBody appendData : imageData];
[PostBody appendData : [bodySuffixString dataUsingEncoding : NSUTF8StringEncoding allowLossyConversion : NO ]];


NSString * baseURL = [ NSString stringWithFormat : @ " http://api.t.sina.com.cn/statuses/upload.xml?source=3930264715 " ];
NSURL * url = [ NSURL URLWithString : baseURL];
NSMutableURLRequest * mainRequest = [[ NSMutableURLRequest alloc ] initWithURL : url
CachePolicy : NSURLRequestUseProtocolCachePolicy
timeoutInterval : 30.0f ];

[MainRequest setHTTPMethod : @ "POST" ];
[MainRequest setValue : authValue forHTTPHeaderField : @ "Authorization" ];
NSString * contentType = [ NSString stringWithFormat : @ "multipart / form-Data; boundary =% @" , boundary, nil ];
[MainRequest setValue : contentType forHTTPHeaderField : @ "Content-Type" ];
[MainRequest setHTTPBody : postBody];

NSURLResponse * shareResponse;
NSError * Error;

NSData * responseData = [ NSURLConnection sendSynchronousRequest : mainRequest returningResponse : & shareResponse Error : & Error];
NSString * responsestring = [[ NSString alloc ] initWithData : responseData encoding : NSUTF8StringEncoding ];

NSHTTPURLResponse * HttpResponse = ( NSHTTPURLResponse *) shareResponse;

int statusCode = [HttpResponse statusCode ];

NSLog ( @ "status code =% d" , statusCode);

BOOL succeed = NO ;
if (statusCode == 200 ) {
succeed = YES ;
}
[MainRequest Release ];


NSLog ( @ "String response:% @" , responsestring);
 
[Responsestring Release ];

[ uploadWaiting stopAnimating ];

NSString * Message = nil ;
if (succeed) {
Message = @ " shared success " ;
} else {
Message = @ " failed to share " ;
}
UIAlertView * Alert = [[ UIAlertView alloc ] initWithTitle : @ "" Message : Message
delegate : self
cancelButtonTitle : @ " OK " otherButtonTitles : nil ];
[Alert Show ];
[Alert Release ];

No comments:

Post a Comment