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.
Related
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.
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 recently setup my windows server to run over HTTPS with an SSL certificate.and I have a problem to redirect HTTP request to https
I add this code in Global.asax
protected void Application_BeginRequest()
{
if (!Context.Request.IsSecureConnection))
Response.Redirect(Context.Request.Url.ToString().Replace("http:", "https:"));
}
but just GET request redirect and POST request failed.
please help me.
The best answer to this is to not redirect this as you desire, rather it would be to redirect to a secure version of the referrer which will submit to an HTTPS URI.
Redirecting a POST request does not forward the data. Your request may have been redirected this way, and your code threw errors due to form values being NULL
The data that was submitted originally was not encrypted when it was originally posted.
You should not ideally redirecting HTTP to HTTPS for POST request. Just don't allow POST request over HTTP. As you should aware POST request can't be use for bookmark or can't be cached.
So there is no meaning redirecting POST request to HTTPS.
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'm using HttpWebRequest to pull down XML, and POST data back to a 'WebService' and getting a 401 on the POST.
When creating the requests I've added Credentials and now tried a credentials cache and setting PreAutenticate to True, still getting the 401! :(
Watching the HTTP traffic on the router I set the get make an unauthenticated GET request.. it hits the 401 and then makes an authenticated GET and is allowed through. When I watch the POST I see it hit the 401... and it doesn't even try an authenticated POST.
This appears only on mobile phones (compact-framework 3.5 and 2.0 on WinMobile 6.1). The same .exe works perfectly on any desktop machines.
What am I missing? Please help!
Try setting the header manually:
http://devproj20.blogspot.com/2008/02/assigning-basic-authorization-http.html