httpwebresponse

Get a collection of redirected URLs from HttpWebResponse

牧云@^-^@ 提交于 2019-11-27 08:02:45
问题 I'm trying to retrieve a list of urls that represent the path taken from URL X to URL Y where X may be redirected several times. For example: http://www.example.com/foo That will redirect to: http://www.example.com/bar Which then redirects to: http://www.example.com/foobar Is there a way of get this redirect pathway from the response object as a string: http://www.example.com/foo > http://www.example.com/bar > http://www.example.com/foobar I'm able to get at the final URL via ResponseUri e.g.

Getting the Response of a Asynchronous HttpWebRequest

心不动则不痛 提交于 2019-11-27 06:55:37
Im wondering if theres an easy way to get the response of an async httpwebrequest. I have already seen this question here but all im trying to do is return the response (which is usually json or xml) in the form of a string to another method where i can then parse it/ deal with it accordingly. Heres some code: I have these two static methods here which i think are thread safe as all the params are passed in and there are no shared local variables that the methods use? public static void MakeAsyncRequest(string url, string contentType) { HttpWebRequest request = (HttpWebRequest)WebRequest

HTTPWebResponse + StreamReader Very Slow

Deadly 提交于 2019-11-27 06:45:55
I'm trying to implement a limited web crawler in C# (for a few hundred sites only) using HttpWebResponse.GetResponse() and Streamreader.ReadToEnd() , also tried using StreamReader.Read() and a loop to build my HTML string. I'm only downloading pages which are about 5-10K. It's all very slow! For example, the average GetResponse() time is about half a second, while the average StreamREader.ReadToEnd() time is about 5 seconds! All sites should be very fast, as they are very close to my location, and have fast servers. (in Explorer takes practically nothing to D/L) and I am not using any proxy.

GetResponseAsync does not accept cancellationToken

谁说胖子不能爱 提交于 2019-11-27 05:16:50
It seems that GetResponseAsync does not accept cancellationToken in Async/Await. So the question is how can I cancel the below procedure, provided I need to collect Cookies from response: using (HttpWebResponse response = (HttpWebResponse) await request.GetResponseAsync()) { cookies.Add(response.Cookies); } An alternative code to achieve the above is also welcome. Something like this should work (untested): public static class Extensions { public static async Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest request, CancellationToken ct) { using (ct.Register(() => request.Abort(),

How to process WebResponse when .NET throws WebException ((400) Bad Request)?

允我心安 提交于 2019-11-27 00:47:56
问题 I'm using Facebook Graph Api and trying to get user data. I'm sending user access token and in case this token is expired or invalid Facebook returns status code 400 and this response: { "error": { "message": "Error validating access token: The session is invalid because the user logged out.", "type": "OAuthException" } } The problem is that when I use this C# code: try { webResponse = webRequest.GetResponse(); // in case of status code 400 .NET throws WebException here } catch (WebException

WebClient vs. HttpWebRequest/HttpWebResponse

坚强是说给别人听的谎言 提交于 2019-11-26 23:21:16
It seems to me that most of what can be accomplished with HttpWebRequest/Response can also be accomplished with the WebClient class. I read somewhere that WebClient is a high-level wrapper for WebRequest/Response . So far, I can't see anything that can be accomplished with HttpWebRequest/Response that can not be accomplished with WebClient , nor where HttpWebRequest/Response will give you more "fine-grained" control. When should I use WebClient and when HttpWebRequest/Response ? (Obviously, HttpWebRequest/Response are HTTP specific.) If HttpWebRequest/Response are lower level then WebClient ,

Sockets in C#: How to get the response stream?

自闭症网瘾萝莉.ら 提交于 2019-11-26 22:52:27
问题 I'm trying to replace this: void ProcessRequest(object listenerContext) { var context = (HttpListenerContext)listenerContext; Uri URL = new Uri(context.Request.RawUrl); HttpWebRequest.DefaultWebProxy = null; HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(URL); httpWebRequest.Method = context.Request.HttpMethod; httpWebRequest.Headers.Clear(); if (context.Request.UserAgent != null) httpWebRequest.UserAgent = context.Request.UserAgent; foreach (string headerKey in context

Why is this WebRequest code slow?

会有一股神秘感。 提交于 2019-11-26 22:23:34
问题 I requested 100 pages that all 404. I wrote { var s = DateTime.Now; for(int i=0; i < 100;i++) DL.CheckExist("http://google.com/lol" + i.ToString() + ".jpg"); var e = DateTime.Now; var d = e-s; d=d; Console.WriteLine(d); } static public bool CheckExist(string url) { HttpWebRequest wreq = null; HttpWebResponse wresp = null; bool ret = false; try { wreq = (HttpWebRequest)WebRequest.Create(url); wreq.KeepAlive = true; wreq.Method = "HEAD"; wresp = (HttpWebResponse)wreq.GetResponse(); ret = true;

How to set response filename without forcing “save as” dialog

泪湿孤枕 提交于 2019-11-26 18:57:38
问题 I am returning a stream in some response setting the appropriate content-type header. The behavior I'm looking for is this: If the browser is able to render content of the given content type then it should display it in the browser window. If the browser doesn't know how to render the content, then it should display the "save as" dialog where the filename should be the one provided in the response. The problem is that if I set the Content-Disposition header with: "attachment; filename="myfile

Get HTTP requests and responses made using HttpWebRequest/HttpWebResponse to show in Fiddler

亡梦爱人 提交于 2019-11-26 18:48:30
Is there any way I can hook Fiddler up to capture requests and responses made using .NET HttpWebRequest and HttpWebResponse? The Fiddler FAQ gives the answer to this. You essentially route your HTTP traffic through Fiddler (i.e. Use Fiddler as a proxy). Here's some links that will help: Fiddler Web Debugging - Configuring Clients Which in turn links to here: Take the Burden Off Users with Automatic Configuration in .NET You can achieve this via some configuration settings in the web.config file (for an ASP.NET application) like so: <system.net> <defaultProxy> <proxy proxyaddress="http://[your