3

I am trying to get an html response from a server(aspx).

NSString *urlstr = [[NSString alloc] initWithFormat:@"http://www.someSiteThatRedirectsWithGetParameters.com?parameter1=1&parameter2=2"];

NSURL *url = [ NSURL URLWithString:urlstr];     
NSMutableURLRequest *urlRequest = [ NSURLRequest requestWithURL: url];

NSData data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];

NSURL *lastURL=[response URL];

NSLog(@"Last url %@", lastURL);

I am getting lastURL as www.someSiteThatRedirectsWithGetParameters.com and therefore not getting the html page for the original requested url.

Why is this so and how can I solve this?

EDIT

This is all what I have tried so far : Get the last redirected url in iOS 5? but I am still not able to achieve what I am trying to i.e : get the html page source of the original GET request(going through redirections) having parameters in the url ?

Community
  • 1
  • 1
Vikas Singh
  • 1,781
  • 7
  • 27
  • 54
  • A site that is expecting parameters should not redirect... It is also preferable to use post-requests, so people can't so easily figure out what you are sending. – Martol1ni May 11 '12 at 10:41
  • I am not the one running the site so cant control what a site should or should not do, I am the one sending the request, hence the question! – Vikas Singh May 11 '12 at 10:53

2 Answers2

1

I am getting lastURL as www.someSiteThatRedirectsWithGetParameters.com and therefore not getting the html page for the original requested url.

If your request is being redirected, it's because the server to which you're connecting is telling you that there is no page at the URL to which you first made the request, and you should use the redirected URL instead. That's the whole point of a redirect response.

If the response that you get is a HTTP 303, the new request will properly be changed from POST to GET. This is sometimes done to avoid cases where refreshing the page would resubmit the original request with undesirable consequences. In this case, you POST your request to some URL, and the server accepts the request but responds by redirecting the client to a different URL which ultimately displays the server's response.

Why is this so and how can I solve this?

I've explained the 'why' above. The "solution" is to accept the response from the server and follow the redirect to the URL that contains the server's response.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Whats confusing is, when I use the original request in a web browser, the requested page does get loaded but the same isnt true when I use the code to get the html data :( – Vikas Singh May 14 '12 at 07:20
  • check out this link : http://www.bookfinder4u.com/IsbnSearch.aspx?isbn=0755315316&mode=direct&option=all&country=usa thats one of those behaving as I mentioned. When you open that link via browser, you'll get the search page but when you request the same page using synchronous/async http request, the data that you'll get will be of the home page ( http://bookfinder4u.com) – Vikas Singh May 14 '12 at 07:22
  • Then you'll need to look at the request that you're sending and the request that the browser is sending to see what's different between them. That may help you figure out why the server responds differently. It might be that the server doesn't recognize the user agent that you're sending, or that the browser doesn't like some other header that's included (or not included) in your request. A tool like [Charles proxy](http://www.charlesproxy.com/) can be invaluable in these situations. – Caleb May 14 '12 at 07:24
  • Even I thought it had something to do with user agent, but the thing works with command line using get page. I'll act as per your suggestion and install Charles Proxy to see whats happening – Vikas Singh May 14 '12 at 07:26
  • Ok. So Charles Proxy gives a 200 response which means it doesnt redirect to homepage if requested via a browser but it does when via ios code. – Vikas Singh May 14 '12 at 07:39
  • The server doesn't magically know where the request is coming from; there's at least one difference between the two requests. You'll need to figure out what it is. – Caleb May 14 '12 at 07:42
0

I would try the approach of sending two requests. Send the first request, then the redirectURL will be the response. Then make a new URL of the response, sending a request to that url with the GET-parameters. I haven't tried it, but I suppose it could work.

Martol1ni
  • 4,684
  • 2
  • 29
  • 39