Sni*_*ips 10 redirect nsurlrequest ios
我有一个经过试验和测试的NSURLRequest(和伴奏)实现,它适用于GET和给定URL的POST.
但是,我现在想要移动URL的目标而不更改应用程序使用的URL,所以我打算通过我的DNS提供程序使用webhop重定向.
这适用于GET请求,但POST只是挂起...没有收到连接响应.
处理重定向的相关iOS方法是,
-(NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)redirectResponse
Run Code Online (Sandbox Code Playgroud)
根据Apple的(处理重定向)文档,
如果委托未实现连接:willSendRequest:redirectResponse:,则允许所有规范更改和服务器重定向.
嗯,这不是我的经验,因为退出这个方法对我不起作用.请求只是挂起而没有响应.
Apple还建议实施willSendRequest(参见上面链接的Apple文档),这再次对我不起作用.我看到了调用,但结果请求只是挂起.
我目前对willSendRequest的实现如下(见下文).这是在重定向之后,但处理请求就好像它是一个GET而不是POST.
我认为问题在于重定向正在失去HTTP请求是POST的事实(可能存在更多问题,例如承载请求Body也是如此?).
我不确定我应该在这做什么.因此,任何有关如何正确处理接收重定向的POST的建议都将受到赞赏.谢谢.
-(NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)redirectResponse
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) redirectResponse;
int statusCode = [httpResponse statusCode];
NSLog (@"HTTP status %d", statusCode);
// http statuscodes between 300 & 400 is a redirect ...
if (httpResponse && statusCode >= 300 && statusCode < 400)
{
NSLog(@"willSendRequest (from %@ to %@)", redirectResponse.URL, request.URL);
}
if (redirectResponse)
{
NSMutableURLRequest *newRequest = [request mutableCopy]; // original request
[newRequest setURL: [request URL]];
NSLog (@"redirected");
return newRequest;
}
else
{
NSLog (@"original");
return request;
}
}
Run Code Online (Sandbox Code Playgroud)
其他信息1
willSendRequest接收的HTTP代码是301 - '永久移动.
使用allHTTPHeaderFields来提取标题字段,我看到他请求我最初提交的标题
HTTP header {
"Content-Length" = 244;
"Content-Type" = "application/json";
}
Run Code Online (Sandbox Code Playgroud)
...并且复制/重定向的请求具有标题,
Redirect HTTP header {
Accept = "*/*";
"Accept-Encoding" = "gzip, deflate";
"Accept-Language" = "en-us";
"Content-Type" = "application/json";
}
Run Code Online (Sandbox Code Playgroud)
...看起来不像原始请求的副本,甚至是超集.
Ste*_*her 13
保持你的原始请求,然后提供自己的willSendRequest:redirectResponse:自定义该请求,而不是与一个苹果提供你的工作.
- (NSURLRequest *)connection: (NSURLConnection *)connection
willSendRequest: (NSURLRequest *)request
redirectResponse: (NSURLResponse *)redirectResponse;
{
if (redirectResponse) {
// The request you initialized the connection with should be kept as
// _originalRequest.
// Instead of trying to merge the pieces of _originalRequest into Cocoa
// touch's proposed redirect request, we make a mutable copy of the
// original request, change the URL to match that of the proposed
// request, and return it as the request to use.
//
NSMutableURLRequest *r = [_originalRequest mutableCopy];
[r setURL: [request URL]];
return r;
} else {
return request;
}
}
Run Code Online (Sandbox Code Playgroud)
通过这样做,您明确忽略了HTTP规范的某些方面:重定向通常应转换为GET请求(取决于HTTP状态代码).但实际上,当从iOS应用程序进行POST时,此行为将更好地为您提供服务.
也可以看看:
| 归档时间: |
|
| 查看次数: |
15211 次 |
| 最近记录: |