0

How do I open a link without opening the browser? I need to send postback using a predefined link

https://example.com/test?parm1=aaaa&parm2=1234

Normally I would do:

Process.Start("https://example.com/test?parm1=aaaa&parm2=1234");

But I need to run it on the server and without opening the browser.

Udara Kasun
  • 2,182
  • 18
  • 25
arti
  • 645
  • 1
  • 8
  • 27
  • Possible duplicate of [Forcing a postback](https://stackoverflow.com/questions/8209944/forcing-a-postback) – Feras Al Sous Sep 06 '18 at 09:16
  • 1
    use HttpClient, https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client – Vilsad P P Sep 06 '18 at 09:25
  • Either use WebRequest (see msdn : https://learn.microsoft.com/en-us/dotnet/api/system.net.webrequest?view=netframework-4.7.2) or HttpRequest. WebRequest does a lot of things automatic but is slower than httprequest because the WebRequest has a view. The a HttpRequest is faster but doesn't add a lot of headers to the request and will fail if you do not add the needed headers. – jdweng Sep 06 '18 at 09:26

2 Answers2

0

Did you try to do like this?

String url = "https://en.wikipedia.org/wiki/Sigiriya";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Timeout = Timeout.Infinite;
request.KeepAlive = true;

long length = 0;
try {               

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
        length = response.ContentLength;
        Stream dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        Console.WriteLine(responseFromServer);                  
    }
} catch (Exception ex) {
   Console.WriteLine(ex);   
}

also simply you can call like this

var client = new WebClient();
var content = ient.DownloadString("https://en.wikipedia.org/wiki/Sigiriya");
Udara Kasun
  • 2,182
  • 18
  • 25
0

Putting together all info I have from you, I've cut the code down to:

string link = "https://example.com/test?parm1=aaaa&parm2=1234";
WebRequest request = WebRequest.Create(link);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

checking result using:

Console.WriteLine(response.StatusDescription);

Thank you guys for help!

arti
  • 645
  • 1
  • 8
  • 27