In C #, is it possible to open a URL in the background without opening a browser?

My code should provide some information to the server using php script.

Basically, I want to call www.sitename.com/example.php?var1=1&var2=2&var3=3 , but I don’t want the browser to open, so Process.Start(URL); will not work.

Since I came to this site in order to find out and not get answers, basically, I will explain what I have done so far and the mistakes that I have received. If you still know the solution, feel free to skip the next part.

I looked around and saw a solution for using POST:

 ASCIIEncoding encoding=new ASCIIEncoding(); string postData="var1=1&var2=2&var3=3"; byte[] data = encoding.GetBytes(postData); // Prepare web request... HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://localhost/site.php"); myRequest.Method = "POST"; myRequest.ContentType="application/x-www-form-urlencoded"; myRequest.ContentLength = data.Length; Stream newStream=myRequest.GetRequestStream(); // Send the data. newStream.Write(data,0,data.Length); newStream.Close(); 

However, I need to use GET not POST . At first, I decided that the solution could be to change myRequest.Method = "POST"; on GET , but this did not work, because GET does not work, it retrieves data from the URL.

So, I tried changing the previous code to:

 HttpwebRequest myRequest= (HttpWebRequest)WebRequest.Create("http://localhost/site.php" + postData); Stream newStream = myRequest.GetRequestStream(); newStream.Close() 

According to the logic, that it will call a URL which (hopefully) initiates a GET_ request to the php script, and then life will be a dandy. However, this led to the following error:

 A first chance exception of type 'System.Net.ProtocolViolationException' occurred in System.dll An unhandled exception of type 'System.Net.ProtocolViolationException' occurred in System.dll Additional information: Cannot send a content-body with this verb-type. 

Any help is appreciated and thanks.

+6
source share
3 answers
 string postData="var1=1&var2=2&var3=3"; // Prepare web request... HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create( "http://yourserver/site.php?" + postData); myRequest.Method = "GET"; var resp =(HttpWebResponse) myRequest.GetResponse(); var result = new StreamReader(resp.GetResponseStream()).ReadToEnd(); 

Or maybe even simpler:

 var data = new WebClient().DownloadString("http://yourserver/site.php?var1=1&var2=2&var3=3"); 

Learn more about WebClient for advanced options.

+5
source

It looks like you went down the right route:

 string postData="var1=1&var2=2&var3=3"; // Prepare web request... HttpwebRequest myRequest= (HttpWebRequest)WebRequest.Create( "http://localhost/site.php?" + postData); // Send the data. myRequest.GetResponse(); 

Please note that I have added ? at the end of site.php .

We don’t need to bother with the flow of requests, since all this concerns the placement of things in the request body - and, as you already said, the GET request has its data in the URL, and not in its body.

+2
source

The easiest way is to use the WebClient class. Using it, just 2 lines of code, just put your URL and use methods like DownloadString .

0
source

Source: https://habr.com/ru/post/947299/


All Articles