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.
The not-equal-to operator ( != ) returns true if the operands don't have the same value; otherwise, it returns false .
C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.
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
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.
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