Change Request Method on Redirect

317 views
Skip to first unread message

ryneezy

unread,
Oct 6, 2009, 12:00:54 PM10/6/09
to ASIHTTPRequest
Hi,

I have a school project on anonymous web searches, so I'm using
ASIHTTPRequest to do a post on the anonymous web proxy http://www.hidemyass.com.
For example, if I wanted to search Google for hello, I'd enter
http://www.google.com/search?q=hello in their form box.

My code looks like:

ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:
[NSURL urlWith:@"http://path.to.hidemy.ass.form.post"]] autorelease];

[request setPostValue:@"http://www.google.com/search?q=hello"
forKey:@"q"];

[request start];
NSData *responseData = [request responseData];

The post seems to go through fine, but hidemyass does a redirect. The
output is Google giving an error saying /search?q=hello is not a valid
POST. I believe ASIFormDataRequest is performing another post on the
redirect.

How do I use ASIHTTPRequest or ASIFormDataRequest to POST, then GET on
the redirect?

Thanks,
Ryan

Ben Copsey

unread,
Oct 6, 2009, 5:31:08 PM10/6/09
to asihttp...@googlegroups.com
Hi Ryan


ASIHTTPRequest will do different things depending on the HTTP status
code it got back before it redirected. A 303 redirect will cause it to
change to GET and dump the request body, but other redirect codes will
keep the same request method as before and preserve the request body.
I seem to remember having a discussion about this, and think this is
probably the correct behaviour.

Anyway, you can change it fairly easily, the logic for redirects is in
the bottom of the readResponseHeadersReturningAuthenticationFailure
method in ASIHTTPRequest.m.

Thanks

Ben

ryneezy

unread,
Oct 6, 2009, 6:49:22 PM10/6/09
to ASIHTTPRequest
Cool thanks for the method name to edit.

I was thinking about it more and I was thinking about

setting shouldRedirect:NO
getting the responseStatusCode
looking for the redirect URL in the header/body (wherever it is)
use ASIHTTPRequest to do an HTTP GET on the redirect URL.

I'll try both ways after work today.

Thanks for the info,
Ryan

On Oct 6, 2:31 pm, Ben Copsey <b...@allseeing-i.com> wrote:
> Hi Ryan
>
>
>
> > I have a school project on anonymous web searches, so I'm using
> > ASIHTTPRequest to do a post on the anonymous web proxyhttp://www.hidemyass.com
> > .
> > For example, if I wanted to search Google for hello, I'd enter
> >http://www.google.com/search?q=helloin their form box.

ryneezy

unread,
Oct 6, 2009, 11:43:29 PM10/6/09
to ASIHTTPRequest
Okay. Both ways worked.

hidemyass was returning a 302 and from Wikipedia

The HTTP response status code 302 Found is the most common way of
performing a redirection.
It is an example of industry practice contradicting the standard HTTP/
1.0 specification (RFC 1945), which required the client to perform a
temporary redirect (the original describing phrase was "Moved
Temporarily"), but popular browsers implemented it as a 303 See Other,
ie. changing the request type to GET regardless of what it had been
originally. Therefore, HTTP/1.1 added status codes 303 and 307 to
disambiguate between the two behaviours. However, the majority of Web
applications and frameworks still use the 302 status code as if it
were the 303.

The code at readResponseHeadersReturningAuthenticationFailure changes
the request method to GET only on 303. I modified it so it changes the
request method to GET for 303 or 302.

Since Wikipedia is not the official HTTP spec, I don't know if you
guys should change it too. But it works for what I need it to do.

Thanks for your help Ben. You're great!

Ryan



On Oct 6, 3:49 pm, ryneezy <ryan.sami...@gmail.com> wrote:
> Cool thanks for the method name to edit.
>
> I was thinking about it more and I was thinking about
>
> setting shouldRedirect:NO
> getting the responseStatusCode
> looking for the redirect URL in the header/body (wherever it is)
> use ASIHTTPRequest to do an HTTP GET on the redirect URL.
>
> I'll try both ways after work today.
>
> Thanks for the info,
> Ryan
>
> On Oct 6, 2:31 pm, Ben Copsey <b...@allseeing-i.com> wrote:
>
>
>
> > Hi Ryan
>
> > > I have a school project on anonymous web searches, so I'm using
> > > ASIHTTPRequest to do a post on the anonymous web proxyhttp://www.hidemyass.com
> > > .
> > > For example, if I wanted to search Google for hello, I'd enter
> > >http://www.google.com/search?q=hellointheir form box.
Reply all
Reply to author
Forward
0 new messages