Xcode 中的 Http Get 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4613649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Http Get Method in Xcode
提问by 9891541
I need to create a Http Get Method in Xcode to connect to a host. The http method with cookie likes that :
我需要在 Xcode 中创建一个 Http Get 方法来连接到主机。带有 cookie 的 http 方法就像这样:
Cookie: RouteID=route.neo_srv02; username=01216332476; __utma=72292239.948454815.1294242721.1294288226.1294301327.5; __utmz=72292239.1294242721.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
I don't how to do that, how to set Cookie and in addition connect via proxy.
我不知道如何做到这一点,如何设置 Cookie 以及通过代理连接。
采纳答案by Syed Absar
//Initiate connection
//发起连接
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"GET"];
//keep adding your headers this way
NSString *accept = [NSString stringWithFormat:@"application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"];
[request addValue:accept forHTTPHeaderField: @"Accept"];
//send request & get response
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
回答by sinh99
For Get Method...Example sending text
对于获取方法...例如发送文本
NSURL * serviceUrl = [NSURL URLWithString:@"http://.......url"];
NSMutableURLRequest * serviceRequest = [NSMutableURLRequest requestWithURL:serviceUrl];
[serviceRequest setValue:@"text" forHTTPHeaderField:@"Content-type"];
[serviceRequest setHTTPMethod:@"GET"];
[serviceRequest setValue:@"asd" forHTTPHeaderField:@"username"];
[serviceRequest setValue:@"asd" forHTTPHeaderField:@"password"];
//Get Responce hear----------------------
NSURLResponse *response;
NSError *error;
NSData *urlData=[NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&response error:&error];
NSString *strdata=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@",strdata);
For Post Method....Sending xml example
对于 Post 方法....发送 xml 示例
NSString * xmlString = @"<Envelope><Body>............</Body></Envelop";
NSURL * serviceUrl = [NSURL URLWithString:@"http://...............url"];
NSMutableURLRequest * serviceRequest = [NSMutableURLRequest requestWithURL:serviceUrl];
[serviceRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
[serviceRequest setHTTPMethod:@"POST"];
[serviceRequest setHTTPBody:[xmlString dataUsingEncoding:NSASCIIStringEncoding]];
// response hear-----------------
NSURLResponse *response;
NSError *error;
NSData *urlData=[NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&response error:&error];
NSString *strdata=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@",strdata);
回答by Claus Broch
You could try to give curl
a look. It can be accessed from the command line as well as in build scripts.
你可以试试看curl
。它可以从命令行以及构建脚本中访问。
Full documentation is available through man curl
on the command line.
可以通过man curl
命令行获得完整的文档。