简体   繁体   中英

pass string from C# Windows Form Application to php webpage

How can I pass some data to a webpage from C#.net? I'm currently using this:

ProcessStartInfo p1 = new ProcessStartInfo("http://www.example.com","key=123");
Process.Start(p1);

but how can I access it from PHP? I tried:

<?php echo($_GET['key']); ?> 

but it prints nothing.

Try passing it with the url itself

ProcessStartInfo p1 = new ProcessStartInfo("http://timepass.comule.com?key=123","");  
Process.Start(p1);

您应该将key参数作为查询字符串:

ProcessStartInfo p1 = new ProcessStartInfo("http://timepass.comule.com?key=123");

I would suggest using the HttpWebRequestClass.

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

This way, you would also have the ability to post data to your page, add auth parameters, cookies etc - in case you might need it.

I'm not sure if this matters in your particular setup, passing data thru the query string is not secure. But if security is an issue as well, I would POST the data thru an SSL connection.

Update:

so if you POST'ed data to your php page like so:

string dataToSend = "data=" + HttpUtility.UrlEncode("this is your data string");
var dataBytes = System.Text.Encoding.UTF8.GetBytes(dataToSend);

HttpWebRequest req = (HttpWebRequest) WebRequest.Create("http://localhost/yourpage.php");
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = dataBytes.Length;
req.Method = "POST";

using (var stream = req.GetRequestStream())
{
    stream.Write(dataBytes, 0, dataBytes.Length);
}

// -- execute request and get response
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
if (resp.StatusCode == HttpStatusCode.OK)
    Console.WriteLine("Hooray!");

you can retrieve it by using the following code in your php page:

echo $_POST["data"]) 

Update 2:

AFAIK, ProcessStartInfo/Process.Start() actually starts a process - in this case, I think it will start your browser. The second parameter is the command line arguments. This information is used by programs so they know how to behave when started (hidden, open a default document etc). Its not related to the Query string in anyway. if you prefer to use Process.Start(), then try something like this:

ProcessStartInfo p1 = new ProcessStartInfo("iexplore","http://google.com?q=test");
Process.Start(p1); 

If you run that, it will open internet explorer and open google with test on the search box. If that were you're page, you could access "q" by calling:

echo $_GET["q"]) 

In my applications i used different method ie using webClient i done it

WebClient client1 = new WebClient();
string path = "dtscompleted.php";//your php path
NameValueCollection formData = new NameValueCollection();
byte[] responseBytes2=null;
formData.Add("key", "123");
try
 {
      responseBytes2 = client1.UploadValues(path, "POST", formData);
 }
catch (WebException web)
 {
      //MessageBox.Show("Check network connection.\n"+web.Message);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM