iPhone HTTP Post 发送数据实现
2010-03-18 16:40:00 来源:WEB开发网写了个函数,只接受发送字符串
-(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:
- ››iphone图片拉伸的几种方法
- ››iphone正则表达式的简单使用
- ››HttpHandler生成随机码、验证码
- ››Http 403错误重现实验及解决方法
- ››iPhone开发Unresolved Symbols CAKeyframeAnimati...
- ››IPhone开发-“此证书是由未知颁发机构签名”解决方...
- ››IPhone开发-整合私钥和证书,生成.p12文件
- ››iPhone应用开发-UIPickerView选取器详解
- ››iphone 获取屏幕的宽度和高度
- ››iPhone读取工程包中的二进制文件
- ››iPhone新手机 不挂YouTube APP
- ››iPhone 获取指定格式的时间和日期
更多精彩
赞助商链接