iOS 上的 http GET

发布于 2025-01-07 11:34:02 字数 1245 浏览 0 评论 0原文

我试图通过 HTTP GET 请求从 PC 上的服务获取答案(图像)。 如果我将请求放入网络浏览器,我会得到请求的图像。如果我尝试在 iPhone 应用程序中获取它,它不起作用。

请求是:

http://192.168.151.82:54000/snapshot?s=<snapshotrequest xmlns=\"http://www.vizrt.com/snapshotrequest\"><videomoderequest><width>880</width><height>495</height></videomoderequest><snapshotdata view=\"all\"/></snapshotrequest>&p=http://192.168.151.82:8580/element_collection/storage/shows/%%257B3646FFAC-4E77-41AB-BDFC-F581D157ABA3%%257D/elements/1000/

我的获取代码是:

NSString *str = [NSString stringWithFormat: @"http://192.168.151.82:54000/snapshot?s=<snapshotrequest xmlns=\"http://www.vizrt.com/snapshotrequest\"><videomoderequest><width>880</width><height>495</height></videomoderequest><snapshotdata view=\"all\"/></snapshotrequest>&p=http://192.168.151.82:8580/element_collection/storage/shows/%%257B3646FFAC-4E77-41AB-BDFC-F581D157ABA3%%257D/elements/1000/"];
NSURL *url = [[NSURL alloc] initWithString:str];
UIImage *img = [ [ UIImage alloc ] initWithData: [ NSData dataWithContentsOfURL: url ] ];

您可以看到,处理了引号和百分号等特殊字符。 我用wireshark在PC上观看网络通信,但没有任何通信。

Im trying to get answer (an image) from a service on a PC with a HTTP GET request.
If I put the request into a webbrowser, I get the requested image. If I try to get it in iPhone app, it doesnt work.

the request is:

http://192.168.151.82:54000/snapshot?s=<snapshotrequest xmlns=\"http://www.vizrt.com/snapshotrequest\"><videomoderequest><width>880</width><height>495</height></videomoderequest><snapshotdata view=\"all\"/></snapshotrequest>&p=http://192.168.151.82:8580/element_collection/storage/shows/%%257B3646FFAC-4E77-41AB-BDFC-F581D157ABA3%%257D/elements/1000/

my code for getting is:

NSString *str = [NSString stringWithFormat: @"http://192.168.151.82:54000/snapshot?s=<snapshotrequest xmlns=\"http://www.vizrt.com/snapshotrequest\"><videomoderequest><width>880</width><height>495</height></videomoderequest><snapshotdata view=\"all\"/></snapshotrequest>&p=http://192.168.151.82:8580/element_collection/storage/shows/%%257B3646FFAC-4E77-41AB-BDFC-F581D157ABA3%%257D/elements/1000/"];
NSURL *url = [[NSURL alloc] initWithString:str];
UIImage *img = [ [ UIImage alloc ] initWithData: [ NSData dataWithContentsOfURL: url ] ];

You can see, that speciel characters like quotes and percentes are handeled.
Im watching network communication on the PC with wireshark and there isn't any communication.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国产ˉ祖宗 2025-01-14 11:34:02

在从参数创建 url 之前,您需要对参数进行 url 编码,

NSString * unencodeParameter = @"<snapshotrequest xmlns=\"http://www.vizrt.com/snapshotrequest\"><videomoderequest><width>880</width><height>495</height></videomoderequest><snapshotdata view=\"all\"/></snapshotrequest>&p=http://192.168.151.82:8580/element_collection/storage/shows/%%257B3646FFAC-4E77-41AB-BDFC-F581D157ABA3%%257D/elements/1000/";

NSString * encodedParameter = (NSString *)CFURLCreateStringByAddingPercentEscapes(
            NULL,
            (CFStringRef)unencodedParameter,
            NULL,
            (CFStringRef)@"!*'();:@&=+$,/?%#[]",
            kCFStringEncodingUTF8 );

NSString *str = [NSString stringWithFormat: @"http://192.168.151.82:54000/snapshot?s=%@",encodedParameter];
NSURL *url = [[NSURL alloc] initWithString:str];
UIImage *img = [ [ UIImage alloc ] initWithData: [ NSData dataWithContentsOfURL: url ] ];

并确保您的 iPhone 与您的计算机位于同一 wifi 上,因为您使用的是本地 IP 地址

You need to url encode your parameters before creating a url from them

NSString * unencodeParameter = @"<snapshotrequest xmlns=\"http://www.vizrt.com/snapshotrequest\"><videomoderequest><width>880</width><height>495</height></videomoderequest><snapshotdata view=\"all\"/></snapshotrequest>&p=http://192.168.151.82:8580/element_collection/storage/shows/%%257B3646FFAC-4E77-41AB-BDFC-F581D157ABA3%%257D/elements/1000/";

NSString * encodedParameter = (NSString *)CFURLCreateStringByAddingPercentEscapes(
            NULL,
            (CFStringRef)unencodedParameter,
            NULL,
            (CFStringRef)@"!*'();:@&=+$,/?%#[]",
            kCFStringEncodingUTF8 );

NSString *str = [NSString stringWithFormat: @"http://192.168.151.82:54000/snapshot?s=%@",encodedParameter];
NSURL *url = [[NSURL alloc] initWithString:str];
UIImage *img = [ [ UIImage alloc ] initWithData: [ NSData dataWithContentsOfURL: url ] ];

And also make sure your iPhone is on same wifi as your computer as you are using local IP address

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文