gpt4 book ai didi

ios - NSUrlConnection、http-post 和 session

转载 作者:行者123 更新时间:2023-11-29 03:26:38 25 4
gpt4 key购买 nike

我有关于 NSUrlConnection 和 http session 的问题。我需要使用 POST 调用 httpS url。我尝试了这段代码:

NSString *bodyData = @"selectTipoVeicolo=A&inputTarga=EN784DC";
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.theUrl.com"]];


[postRequest setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];

// Designate the request a POST request and specify its body data
[postRequest setHTTPMethod:@"POST"];
[postRequest setHTTPBody:[NSData dataWithBytes:[bodyData UTF8String] length:strlen([bodyData UTF8String])]];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:postRequest
delegate:self];

[connection start];

但是我获得了一个包含以下消息的 html 页面:

Your session has timed out. Web applications store information about what you are doing on the server. This information is called the session.

Web servers must track many, many sessions. If you are inactive for a long enough time (usually, a few minutes), this information is discarded to make room for active users.

At this point you may restart the session to continue.

编辑

服务器响应:


<NSHTTPURLResponse: 0x8ad8400> { URL: theHost.com/http://voas.theHost.com:7777/… } { status code: 200, headers { "Cache-Control" = private; Connection = "Keep-Alive"; "Content-Length" = 2383; "Content-Type" = "text/html;charset=UTF-8"; Date = "Wed, 04 Dec 2013 21:21:52 GMT"; "Keep-Alive" = "timeout=50,max=9"; Server = "Oracle-Application-Server-10g/9.0.4.3.0 Oracle-HTTP-Server OracleAS-Web-Cache-10g/9.0.4.3.0 (N)"; "Set-Cookie" = "text cookie"; } }

如何避免这个问题?我需要什么?

最佳答案

您还可以通过捕获响应来检查服务器在 header 中返回的内容:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
_data = [[NSMutableData alloc] init];
_response = (NSHTTPURLResponse*) response;
}

您可以在此处或在 connectionDidFinishLoading 中检查响应变量。这应该可以让您了解缺失的部分。就目前情况而言,这可能是服务器问题,并且尚不清楚客户端期望发送什么内容。

顺便说一句,无需启动连接。 init 方法已经启动它了。来自文档:

Returns an initialized URL connection and begins to load the data for the URL request.

关于ios - NSUrlConnection、http-post 和 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20385938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com