Rectangle 27 0

ios How to send a Http post request in objective c?


Google has already written (or generated) a lot of API in Objective-C, which can be found in the link above. In these APIs, almost all parts network affairs are implemented for you, and you can just apply something like username or list name for your situation, and call a simple method to finish your job. That means you can get rid of the painful post/get/delete and write the request by your self. You can check these OC APIs of Youtube in detail by this link.

Maybe you can check about this Objective-C API set provided by Google. Write all the REST things could be painful.

Or if you want to implement the request by yourself, you can try some network frameworks, such as AFNetworking or MKNetworkKit. I guess you will not want to use the Apple's network SDK, which is really hard to use.

What i think onevcat is trying to say is to use a wrapper to construct your calls. Building the request, adding all headers, etc could be costly as time and code. You'd better check out the library Google provides in this regard. code.google.com/p/gdata-objectivec-client Edit: looks like onevcat explained himself already :)

Yes, you're right. For some special reasons, my team is implement request by ourself. The framework We are using is: ASIHTTPRequest

Note
Rectangle 27 0

ios How to send a Http post request in objective c?


ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:strConnection]];
[request setRequestMethod:@"POST"];
[request setPostValue:@"2" forKey:@"GData-Version"];
[request setPostValue:@"application/atom+xml" forKey:@"Content-Type"];
[request setPostValue:YOUTUBE_DEVELOPER_KEY    forKey:@"X-GData-Key"];
[request setPostValue:[[delegate userInfo   ]accessTokenYoutube]  forKey:@"Authorization"];

NSString *body = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom'><id>%@</id></entry>",strVideoID];
[request appendPostData:[body dataUsingEncoding:NSUTF8StringEncoding]];

[request setDelegate:self];
[request setDidFinishSelector:@selector(didAddVideoToFavoriteFinish:)];
[request setDidFailSelector:@selector(didAddVideoToFavoriteFail:)];\
[request setDidReceiveDataSelector:@selector(didAddVideoToFavoriteSelect:)];
[request startAsynchronous];

[request release];

Instead of the ASIFormDataRequest class, you should use the ASIHTTPRequest class. Your code should more or less look like this:

Your POST request is a form data request, which is usually used post the data of an HTML form. It uses a special encoding for data. That's not what the YouTube API expects. It expects a simple POST request with an XML document.

do we have a setPostValue method in ASIHTTPRequest? How this is worked for u? @Codo

Note
Rectangle 27 0

ios How to send a Http post request in objective c?


ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:strConnection]];
[request setRequestMethod:@"POST"];
[request setPostValue:@"2" forKey:@"GData-Version"];
[request setPostValue:@"application/atom+xml" forKey:@"Content-Type"];
[request setPostValue:YOUTUBE_DEVELOPER_KEY    forKey:@"X-GData-Key"];
[request setPostValue:[[delegate userInfo   ]accessTokenYoutube]  forKey:@"Authorization"];

NSString *body = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom'><id>%@</id></entry>",strVideoID];
[request appendPostData:[body dataUsingEncoding:NSUTF8StringEncoding]];

[request setDelegate:self];
[request setDidFinishSelector:@selector(didAddVideoToFavoriteFinish:)];
[request setDidFailSelector:@selector(didAddVideoToFavoriteFail:)];\
[request setDidReceiveDataSelector:@selector(didAddVideoToFavoriteSelect:)];
[request startAsynchronous];

[request release];

Instead of the ASIFormDataRequest class, you should use the ASIHTTPRequest class. Your code should more or less look like this:

Your POST request is a form data request, which is usually used post the data of an HTML form. It uses a special encoding for data. That's not what the YouTube API expects. It expects a simple POST request with an XML document.

do we have a setPostValue method in ASIHTTPRequest? How this is worked for u? @Codo

Note