ios开发 怎么发送post请求
1个回答
展开全部
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15];
[request setHTTPMethod:@"POST"];
NSString *parametersStr = [self getPostStr:parameters];//eg :@"city=sz&name=jack"
NSData *data = [parametersStr dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
////////////
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[connection start];
/////////
各种connection回调。。。。
就不详细说明了
#pragma mark - 网络请求回调
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.mTempData = [[NSMutableData alloc]initWithCapacity:10];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.mTempData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
self.recievedData = self.mTempData;
NSString *jsonString = [[NSString alloc]initWithData:self.recievedData encoding:NSUTF8StringEncoding];
NSLog(@"HttpCallBack_connectionRecieved Data:%@",jsonString);
//将字符串写到缓冲区。
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
//解析json数据,使用系统方法 JSONObjectWithData: options: error:
NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSUTF8StringEncoding error:nil];
if ([_target respondsToSelector:@selector(HttpConnectionCallback:path:)]) {
dispatch_async(dispatch_get_main_queue(), ^{
[_target HttpConnectionCallback:dic path:_path];
});
}
}
//failure
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if ([_target respondsToSelector:@selector(HttpConnectionFailed:path:)]) {
dispatch_async(dispatch_get_main_queue(), ^{
[_target HttpConnectionFailed:error path:_path];
});
}
}
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15];
[request setHTTPMethod:@"POST"];
NSString *parametersStr = [self getPostStr:parameters];//eg :@"city=sz&name=jack"
NSData *data = [parametersStr dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
////////////
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[connection start];
/////////
各种connection回调。。。。
就不详细说明了
#pragma mark - 网络请求回调
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.mTempData = [[NSMutableData alloc]initWithCapacity:10];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.mTempData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
self.recievedData = self.mTempData;
NSString *jsonString = [[NSString alloc]initWithData:self.recievedData encoding:NSUTF8StringEncoding];
NSLog(@"HttpCallBack_connectionRecieved Data:%@",jsonString);
//将字符串写到缓冲区。
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
//解析json数据,使用系统方法 JSONObjectWithData: options: error:
NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSUTF8StringEncoding error:nil];
if ([_target respondsToSelector:@selector(HttpConnectionCallback:path:)]) {
dispatch_async(dispatch_get_main_queue(), ^{
[_target HttpConnectionCallback:dic path:_path];
});
}
}
//failure
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if ([_target respondsToSelector:@selector(HttpConnectionFailed:path:)]) {
dispatch_async(dispatch_get_main_queue(), ^{
[_target HttpConnectionFailed:error path:_path];
});
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询