echolocation
echolocation

Reputation: 1130

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

My code needs to supply some information to a server via a 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); won't work.

Since I come to this site to learn and not to get answers, mostly, I will explain what I've done so far and the errors I have gotten. If you know a solution anyway, feel free to skip the next part.

I have looked around, and I 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 require the use of GET not POST. At first I thought the solution might be to change myRequest.Method = "POST"; to GET, but this didn't work because that's not how GET works, it pulls data from the URL.

So, then I attempted to change the previous code to:

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

Under the logic that it would call the URL, which would (hopefully) initiate the GET_ request on the php script, and then life would be dandy. This however resulted in 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.

Upvotes: 7

Views: 14416

Answers (3)

rene
rene

Reputation: 42463

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");

See the WebClient class for more options

Upvotes: 5

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239734

You mostly seem to have gone 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();

Note that I've added the ? at the end of site.php.

We don't have to fiddle around with the request stream since that's all about putting things in the body of a request - and as you've stated, a GET request has its data in the URL, not in its body.

Upvotes: 2

Yuriy Galanter
Yuriy Galanter

Reputation: 39777

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

Upvotes: 0

Related Questions