WEB开发网
开发学院手机开发iPhone 开发 iPhone HTTP Post 发送数据实现 阅读

iPhone HTTP Post 发送数据实现

 2010-03-18 16:40:00 来源:WEB开发网   
核心提示:写了个函数,只接受发送字符串-(NSString *)MyUrl:(NSString *)urlStr{NSURL *url;NSMutableURLRequest *urlRequest;NSMutableData *postBody = [NSMutableData data];url = [NSURL URLWi

写了个函数,只接受发送字符串

-(NSString *)MyUrl:(NSString *)urlStr

{

NSURL *url;

NSMutableURLRequest *urlRequest;

NSMutableData *postBody = [NSMutableData data];

url = [NSURL URLWithString:@"http://ceshixieyi.appspot.com/ceshixieyi..."];

urlRequest = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];

[urlRequest setHTTPMethod:@"POST"];

//NSString *udid = @"U0020 #999999#E3CEB5881A0A1FDAAD01296D7554868D#";

[postBody appendData:[urlStr dataUsingEncoding: NSUTF8StringEncoding allowLossyConversion:YES]];

[urlRequest setHTTPBody:postBody];

NSData *returnData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];

NSString *result = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

if(returnData)

{

NSLog(@"%@",result);

}

else

{

NSLog(@"error!");

}

return result;

}

是这样调用的NSString *udid = @"U0020 #999999#E3CEB5881A0A1FDAAD01296D7554868D#";

[self MyUrl:udid];

因为手头的一个项目,要求发送udid这样的字符串,而且 U0020代码部分要求8位,但是我起初没有加空格,搞了好久,所以印象好深刻,最后安慰自己买了雪糕...

中间发现了一个外国人写好的,网上一艘就有,叫什么ASIHTTPRequest的,

ASIHTTPRequest is an easy to use wrapper around the CFNetwork API that makes some of the more tedious aspects of communicating with web servers easier. It is written in Objective-C and works in both Mac OS X and iPhone applications.

It is suitable performing basic HTTP requests and interacting with REST-based services (GET / POST / PUT / DELETE). The included ASIFormDataRequest subclass makes it easy to submit POST data and files using multipart/form-data.

It provides:

1 2  下一页

Tags:iPhone HTTP Post

编辑录入:coldstar [复制链接] [打 印]
赞助商链接