Xcode 中的 HTTP 获取方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5975112/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:01:19  来源:igfitidea点击:

HTTP get method in Xcode

xcodehttpmethodsget

提问by user730581

I want to upload to an internet resource 2 values

我想上传到互联网资源 2 个值

I have created a php function "http://resource/top/?artist=VALUE1&title=VALUE2"

我创建了一个 php 函数“http://resource/top/?artist=VALUE1&title=VALUE2”

But I have a little bit lost how to develop this on Xcode side

但是我有点迷失了如何在 Xcode 方面开发它

Many thanks for your answers

非常感谢您的回答

回答by Tendulkar

you can take this as an example

你可以以此为例

NSString *post=[NSString stringWithFormat:@"userId=%@&password=%@&device=%@&amount=%@",[Login retuserid],[passwordfield text],[Login retdevid],[amountfield text]];
        NSLog(@"post string is :%@",post);
        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
        [request setURL:[NSURL URLWithString:@"http://203.217.146.44:81/POS/api.php?fn=cashRequest"]];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];

        NSData *serverReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        NSString *replyString = [[NSString alloc] initWithBytes:[serverReply bytes] length:[serverReply length] encoding: NSASCIIStringEncoding];
        NSLog(@"reply string is :%@",replyString);

回答by mvds

There are several ways to retrieve a URL, some easier and some harder to implement. As a quick start, for the unexperienced developer, this oneliner should do:

有几种方法可以检索 URL,有些更容易实现,有些更难。作为一个快速开始,对于没有经验的开发人员,这个 oneliner 应该做:

NSString *s=[NSString stringWithContentsOfURL:[NSURL urlWithString:@"http://url/..."]];

To do it the proper way (checking for errors, and not blocking your program) have a look at NSURLConnection, or e.g. ASIHTTPRequest which bundles a lot of functionality.

要以正确的方式(检查错误,而不是阻止您的程序)执行此操作,请查看 NSURLConnection,或例如捆绑了许多功能的 ASIHTTPRequest。