意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

Objective-C的Http请求(转)

来源:恒创科技 编辑:恒创科技编辑部
2022-09-14 21:00:00


//prepar request

NSString *urlString = [NSString stringWithFormat:@"http://urlToSend.com"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

//set headers
NSString *contentType = [NSString stringWithFormat:@"text/xml"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

//create the body
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"<xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<yourcode/>"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"</xml>"] dataUsingEncoding:NSUTF8StringEncoding]];

//post
[request setHTTPBody:postBody];

//get response
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
NSLog(@"Response: %@", result);

//here you get the response

}
//该代码片段来自于: http://www.sharejs.com/codes/objectc/2526



Objective-C的Http请求(转)

​​http://www.sharejs.com/codes/objectc/2526​​


另外的一个文章中是这样写的:


post请求:



//post数据到地址url
-(NSString*)httpPostDataWithUrl:(NSString*)url postData:(NSString*)data
{
//使用工厂方法创建
// NSURLRequest *request = [NSURLRequest requestWithURL:url];
// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//同时设置缓存策略和超时时间
NSMutableURLRequest *[NSURLRequest requestWithURL:[NSURL URLWithString:[_mainUrl stringByAppendingString:url]] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
//设置Http头
NSDictionary *headers = [request allHTTPHeaderFields];
[headers setValue:@"iOS-Client-ABC" forKey:@"User-Agent"];
//设置请求方法
//[request setHTTPMethod:@"GET"];
setHTTPMethod:@"POST"];
//设置要发送的正文内容(适用于Post请求)
//NSString *content = @"username=stanyung&password=123";
NSData *contentData = [data dataUsingEncoding:NSUTF8StringEncoding];
setHTTPBody:contentData];
//同步执行Http请求,获取返回数据
NSURLResponse
NSError *error = nil;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//返数据转成字符串
NSString *html = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
NSLog(@"get html:%@",html);
//(如果有错误)错误描述
if
NSString *errorDesc = [error localizedDescription];
NSLog(@"%@",errorDesc);
}

//获取状态码和HTTP响应头信息
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse
NSInteger statusCode = [httpResponse statusCode];
NSLog(@"statusCode:%i",statusCode);
NSDictionary *responseHeaders = [httpResponse allHeaderFields];
NSString *cookie = [responseHeaders valueForKey:@"Set-Cookie"];
NSLog(@"cookie:%@",cookie);
return
}

get请求

-(NSString*)httpGetDataWithUrl:(NSString*)url
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[_mainUrl stringByAppendingString:url]]];
NSDictionary *headers = [request allHTTPHeaderFields];
[headers setValue:@"iOS-Client-ABC" forKey:@"User-Agent"];

//同步执行Http请求,获取返回数据
NSURLResponse
NSError *error = nil;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//返数据转成字符串
NSString *html = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
NSLog(@"get html:%@",html);
//(如果有错误)错误描述
if
NSString *errorDesc = [error localizedDescription];
NSLog(@"%@",errorDesc);
}

//获取状态码和HTTP响应头信息
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse
NSInteger statusCode = [httpResponse statusCode];
NSLog(@"statusCode:%i",statusCode);
NSDictionary *responseHeaders = [httpResponse allHeaderFields];
NSString *cookie = [responseHeaders valueForKey:@"Set-Cookie"];
NSLog(@"cookie:%@",cookie);
return
}




上一篇: 租用美国服务器:潜在的风险与应对策略。 下一篇: MongoDB 5.0 扩展开源文档数据库操作