NSURLRequest:如何处理重定向的帖子? [英] NSURLRequest: How to handle a redirected post?

查看:108
本文介绍了NSURLRequest:如何处理重定向的帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个经过试验和测试过的NSURLRequest(和伴奏)实现,适用于GET和给定URL的POST。

I have a tried and tested use of NSURLRequest (and accompaniments) implementation, that works great for GETs, and POSTs for a given URL.

但是,我想要现在移动URL的目标而不更改应用程序使用的URL,所以我打算通过我的DNS提供程序使用webhop重定向。

However, I want to now move the target of the URL without changing the URL used by the app, so I'm intending to use a webhop redirect via my DNS provider.

这很好用对于GET请求,但POST只是挂起...没有收到连接响应。

This works fine for the GET requests, but the POSTs just hang... no connection response is received.

用于处理重定向的相关iOS方法是,

The relevant iOS method for handling a redirect is,

-(NSURLRequest *)connection:(NSURLConnection *)connection
    willSendRequest:(NSURLRequest *)request
    redirectResponse:(NSURLResponse *)redirectResponse

根据Apple的文档(处理重定向),

According to Apple's documentation for (handling redirects),

如果代表没有实现连接:willSendRequest:redirectResponse:,允许所有规范更改和服务器重定向。

嗯,这不是我的经验,因为保留此方法不为我工作。该请求只是挂起而没有响应。

Well, that's not my experience, because leaving this method out does not work for me. The request just hangs without a response.

Apple还建议实施willSendRequest(参见上面链接的Apple文档),这对我来说也不起作用。我看到了调用,但结果请求只是挂起。

Apple also suggests an implementation, of willSendRequest (see above linked Apple documentation), again this doesn't work for me. I see the invocations, but the resulting requests just hang.

我当前的willSendRequest实现如下(见下文)。这是在重定向之后,但处理请求就好像它是一个GET而不是POST。

My current implementation of willSendRequest is as follows (see below). This follows the redirect, but the handles the request as if it was a GET, rather than a POST.

我认为问题在于重定向正在失去以下事实: HTTP请求是一个POST(可能会有更多问题,比如承载请求Body也是如此?)。

I believe the problem is that the redirection is losing the fact that the HTTP request is a POST (there may be more problems, such as carrying the request Body forward too?).

我不知道我应该在这做什么。因此,任何关于如何正确处理接收重定向的POST的建议都将受到赞赏。谢谢。

I'm not sure what I should be doing here. So any advice on how to correctly handle a POST that receives a redirect would be appreciated. Thanks.

-(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;
    }
}

附加信息1

willSendRequest收到的HTTP代码是301 - '永久移动。

The HTTP code received by willSendRequest is 301 - 'Moved Permanently.

使用allHTTPHeaderFields提取标题字段,我看到他要求我最初提交的标题

Using allHTTPHeaderFields to extract the header fields, I see that he request I originally submit has the header

HTTP header {
  "Content-Length" = 244;
  "Content-Type" = "application/json";
}

...复制/重定向的请求有标题,

...and the copied / redirected request has the header,

Redirect HTTP header {
  Accept = "*/*";
  "Accept-Encoding" = "gzip, deflate";
  "Accept-Language" = "en-us";
  "Content-Type" = "application/json";
}

...它看起来不像原始请求的副本,或者甚至是一个超集。

...which doesn't look like a copy of the original request, or even a superset.

推荐答案

保留原始请求,然后提供你自己的 willSendRequest:redirectResponse:自定义那个请求,而不是与Apple提供的那个一起使用。

Keep your original request, then provide your own willSendRequest:redirectResponse: to customize that request, rather than working with the one Apple provides you.

- (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;
    }
}

通过这样做,你明确忽略了某些方面HTTP规范:重定向通常应转换为GET请求(取决于HTTP状态代码)。但实际上,当从iOS应用程序进行POST时,此行为将更好地为您提供服务。

By doing this, you're explicitly ignoring some aspects of the HTTP spec: Redirects should generally be turned into GET requests (depending on the HTTP status code). But in practice, this behaviour will serve you better when POSTing from an iOS application.

另请参阅:

  • iOS Developer Library, URL Loading System Programming Guide: Handling Redirects and Other Request Changes

这篇关于NSURLRequest:如何处理重定向的帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆