I see a lot of articles talking about how to use it. But what is the advantage of using Response.redirect in your c#, versus just using ahref in the .aspx file?
Response.Redirect runs on the ASP.NET Server. It can only be using when the server is processing an existing HTTP request. It sends a redirect status code and location to redirect back to the client as part of an HTTP response. Then client can then take that location and send an HTTP request to it. So you end up with:
Client -> Server: Initial request for resource at location X
Server -> Client: Redirect to get resource from location Y
Client -> Server: Request for resource at location Y
A hyperlink (a href) is before any HTTP request is even generated. When you click on a hyperlink, the browser just sends an HTTP request to the location specified by the href. The browser could then response with a redirect.
They're really apples and oranges, because they operate at different times.
Related
Is there a way to capture the last landing page's HTTP response/HTML in .NET?
This is what happens:
Use HttpClient to POST www.website.com/login
/login responds with a new location
App JavaScript handles the new location by using window.location = www.website.com/newLocation
I want to capture the response where it was redirected to
When I use HttpClient, it is only able to capture the new location in Step 2 and it will not capture anything further after that.
Is there a way to capture HTTP response in Step 4? I'm specifically after the HTML code that is passed back to the browser.
I have a solution with two ASP.NET Core MVC projects. One project (Client) is making a request to the other (Server) using HttpClient. When the action in Server receives the request, I want to get the URL of the thing that sent it. Every article I have read purports Request.Headers["Referer"] as the solution, but in my case Headers does not contain a "referer" key (or "referrer").
When receiving the request in Server, how should I find the URL of the Client that sent it?
That is how you you get the referring url for a request. But the referer isn't the thing that sent the request. The referer gets set in the headers by the browser when a person clicks on a link from one website to go to another website. When that request is made by the browser to the new website the request will typically have the Referer header which will contain the url of the prior website.
The receiving server can't get the url of the "client" making the request, remember a typical web browser client isn't at any url. All the receiving server can get is the IP address of the client typically.
Since you have control of the client software, if you wanted you could have the client put whatever info you want in the header of the request before it's sent to the server and the server could then get that info out of the header.
If you're using HttpClient, then it is up to the site making the request to add that header. It isn't added automatically in this case. So: change the code - or request that the code is changed - so as to add the header and value that you expect. If you are proxying through a request, you might get the value from the current request's Referer header, and add that.
Even in the general case of a browser making the request as part of a normal page cycle, you can't rely on it: the Referer header is often deliberately not sent; depending on the browser version, configuration, whether you're going between different domains, whether it is HTTPS or not, and rel markers on a <a href=... such as "noreferrer".
I'm trying to use YoutubeFisher library with ASP.NET. I make an HttpWebRequest to grab html content, process the contest to extract the video links and display links on the web page. I managed to make it work on localhost. I can retrieve video links and download the video on the locahost. But when I push it to the Server, it works only if I send the request from the same Server. If that page is accessed by a client browser, the client can see the links properly, but when link is clicked the client gets the HTTP Error 403, everytime the client clicks on the link even though the link is correct.
My analysis is that when the Server makes HttpWebRequest to grab HTML contet, it sends HTTP header as well. The HTML content (links to the video file) that is sent back from YouTube server, I think, will reponse to only the request that matches that HTTP header, that is sent from the Server. So, when client clicks on the link it sends request to YouTube server with different HTTP header.
So, I'm thinking of getting the HTTP header from the client, then modify the Server HTTP header to include HTTP header info of the client before making HttpWebRequest. I'm not quite sure if this will work. As far as I know, HTTP heaer cannot be modified.
Below is the code that makes HttpWebRequest from YouTubeFisher library,
public static YouTubeService Create(string youTubeVideoUrl)
{
YouTubeService service = new YouTubeService();
service.videoUrl = youTubeVideoUrl;
service.GetVideoPageHtmlSource();
service.GetVideoTitle();
service.GetDownloadUrl();
return service;
}
private void GetVideoPageHtmlSource()
{
HttpWebRequest req = HttpWebRequest.Create(videoUrl) as HttpWebRequest;
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
videoPageHtmlSource = new StreamReader(resp.GetResponseStream(), Encoding.UTF8).ReadToEnd();
resp.Close();
}
Client browses the page but the links are there but give HTTP 403:
Browse the page the from the Server itself, everything works as expected:
How do I make HttpWebRequest on the behalf of the client then? Is my analysis of this problem correct?
Thank you for your input.
Use an http monitor such as Charles, Fiddler or even Firebug to find out what additional headers are being sent from the brower in the success case. I suspect you'll need to duplicate one or more of accept, user-agent or referer.
In the past I've just assumed that youtube has those links encoded so that they only work for the original request IP. If that were the case it would be far more difficult. I have no clue if this is the case or not, try forwarding all the header elements you can before going down this route...
The only possibility that comes to mind is that you'd have to use a javascript request to download the page to the client's browser, then upload that to your server for processing, or do the processing in javascript.
Or you could have the client download the video stream via your server, so your server would pass through the data. This would obviously use a ton of your bandwidth.
I have a C# app to log on a website and collects some data. The server (Apache) sometimes sends back the 302 code whereupon the C# application sends a request again to the server with the same URI. The problem is the Apache stops with 'W' status (HttpClient stuck on request).
I would like to know if I send a request with the response.ResponseURI that it would be solve this problem, and why the server sends the code 302 so on erratic way? Someone said me the Apache answers this code (302) when the content of the requested page was not changed. ???
Help me, please?
Check http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html . If you have a 302 code response you should redo a request with the URI indicated by the Location: header of the HTTP server response.
This is a temporary redirect.
Redirect are quite common in response to POST requests (to be perfect it should be a 303 redirect...) and it's quite certainly done to redirect you to the home page url for logged in users OR to the login form if you failed your login.
I can open an image from WebClient.DownloadData but for some urls which am able to open via web-browser, can't able to get image byte array if used via WebClient.DownloadData.
If i can view via browser, why do i get http 404 file not found exception.
WebException was unhandled.
The remote server returned an error: (404) Not Found.
Thanks.
If the URL is valid perhaps they have some anti leaching mechanisms e.g. checking referer or cookie.
You can use something like Fidler to inspect web browser requests and ensure that your download requests are the same.
Make sure your request is same/similar to browser's one.
The easiest way is to use Fiddler and set it to log requests from all processes, than run the request from a browser and from you application. Look for differences between url, method and headers for 2 requests to see what is causeing it. (You many need to configure your program to use local proxy for web requests to get HTTP requests traced through Fiddler).
Try using this code. Set the Referer to something like google.com http://www.pastie.org/1429985