iOS POST请求
来源:互联网 发布:php超链接输出成文本 编辑:程序博客网 时间:2024/05/21 21:39
用post请求向服务器发送一个json格式的URL; 用post向服务器发送数据 切记是以表单的形式 向服务器发送数据 get则是在url后面追加字符串,拼接字符串;
post格式的URL:
NSString * Pass = [_passWord.textstringByAppendingString:_radString];
NSString * strMD2=[[MyMD5md5:Pass] uppercaseString];
NSString * postString = [NSStringstringWithFormat:@"param={\"Email\":\"%@\",\"RealName\":\"%@\",\"Pwd\":\"%@\",\"Rad\":\"%@\"}",_emailText.text,_userName.text,strMD2,_radString];
NSLog(@"------%@-------",postString);
//往服务器上传要转成ASCII
NSData * postData = [postStringdataUsingEncoding:NSASCIIStringEncodingallowLossyConversion:YES];
NSMutableURLRequest * request = [[NSMutableURLRequestalloc]init];
[request setURL:[NSURLURLWithString:@"http://192.168.0.140:91/App/Login/Reg"]];
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
[requestsetTimeoutInterval:20.0f];
[requestsetHTTPMethod:@"POST"];
[requestsetHTTPBody:postData];
//application/x-www-form-urlencoded告诉服务器是一个表单提交数据放式
[request setValue:@"application/x-www-form-urlencoded"forHTTPHeaderField:@"Content-Type"];
//得到提交数据的长度
NSString * len = [NSStringstringWithFormat:@"%d",[postDatalength]];
//添加一个http包头告诉服务器数据长度是多少
[request setValue:lenforHTTPHeaderField:@"Content-Length"];
[NSURLConnectionconnectionWithRequest:request delegate:self];
#pragma mark --
#pragma mark -- NSMutableRequestDelegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
_buffer = [[NSMutableDataalloc]init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_bufferappendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString * info = [[NSStringalloc]initWithData:_bufferencoding:NSASCIIStringEncoding];
NSLog(@"-----info--%@------",info);
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"-----错误提示--%@--",[errorlocalizedDescription]);
}
- IOS-post请求、get请求
- iOS post请求 (普通请求)
- iOS get post请求
- IOS Get、Post请求
- iOS POST请求
- ios HTTP post请求
- ios get post请求
- IOS Get、Post请求
- iOS 创建post请求
- IOS异步请求 post
- ios-post请求
- iOS中POST请求
- iOS同步post请求
- ios NSURLSession Post请求
- IOS发送POST/GET请求
- [ios]get,post请求 【转】
- iOS 数据请求 get、post
- iOS 关于post异步请求
- 堆 和 栈的 区别(经典)
- (转)Mac OS X 访问 Windows 共享文件夹
- 致青春
- 软件控制屏幕亮灭
- 让sublime text 2更好地支持Python
- iOS POST请求
- (24)动态扩展数组边界
- 10300 - Ecological Premium
- 自定义ViewGroup实现水平滑动
- 关于思考VirtualAlloc申请的内存属性的问题
- HTML5视频插件,各种已解决和正在解决的问题
- Redis+Keepalived高可用方案详细分析
- UVa 11991 - Easy Problem from Rujia Liu?
- 快速排序