I have a situation where I want to simulate a web request which comes to application. It contains some url values and Request Headers.
I know that I can start a browser using
var url = "http://test.com";
var sInfo = new ProcessStartInfo(url);
Process.Start(sInfo);
But I want to add some header values in the url which I want to open in browser. I have tried using different things but not able to open it in browser.
I have used WebClient as below
WebClient client = new WebClient();
var url = "http://test.com";
client.Headers.Add("USER", "ABC");
string text=client.DownloadString(url);
But how to use this string in web browser I don't know.
I have also tried WebBrowser but not able to simulate.
There is no standard for this. If you want to pass custom headers, you need to consult the web browser you are using. I don't think any of the major browsers have such a feature, though - however, there extensions for both Chrome and Firefox that allow you to globally add headers to every request. Maybe that's good enough for you, maybe not.
Based on the information you provided I see three options to get close to what you want:
If you have a raw html text you can set the property DocumentText of the webBrowser control to render that. The control takes care of loading additional resources but it fails to load resources that are relative to the document source. But this might not be an issue for your use case.
WebClient client = new WebClient();
var url = "http://stackoverflow.com";
client.Headers.Add("USER", "ABC");
string text = client.DownloadString(url);
this.webBrowser1.ScriptErrorsSuppressed = true;
this.webBrowser1.DocumentText = text;
The Navigate method has an overload that takes an additional header parameter.
this.webBrowser1.ScriptErrorsSuppressed = true;
this.webBrowser1.Navigate("http://stackoverflow.com",
null,
new byte[]{},
"USER: ABC;");
Here is what the headers will look like
If you're only interested in parts of the returned html and can afford spending time scraping that html and build-up your own UI you can leverage CsQuery that is a jQuery port to .Net.
WebClient client = new WebClient();
var url = "http://stackoverflow.com";
client.Headers.Add("USER", "ABC");
string text = client.DownloadString(url);
var csdoc = CsQuery.CQ.CreateDocument(text);
foreach(var q in csdoc.Find("a.question-hyperlink"))
{
Debug.WriteLine(q.InnerText);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With