将C#Windows窗体应用程序中的字符串传递给php网页

时间:2021-08-05 15:50:45

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

如何将一些数据从C#.net传递到网页?我目前正在使用这个:

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

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

但是如何从PHP访问它?我试过了:

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

but it prints nothing.

但它什么都不打印。

4 个解决方案

#1


1  

Try passing it with the url itself

尝试使用url本身传递它

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

#2


1  

you should put the key parameter as a query string :

你应该把key参数作为查询字符串:

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

#3


1  

I would suggest using the HttpWebRequestClass.

我建议使用HttpWebRequestClass。

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

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.

这样,您还可以将数据发布到页面,添加身份验证参数,cookie等 - 以防您可能需要它。

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.

我不确定这在您的特定设置中是否重要,通过查询字符串传递数据是不安全的。但如果安全性也是一个问题,我会通过SSL连接POST数据。

Update:

更新:

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

所以,如果你将数据发布到你的php页面,就像这样:

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:

您可以使用php页面中的以下代码检索它:

echo $_POST["data"]) 

Update 2:

更新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:

AFAIK,ProcessStartInfo / Process.Start()实际上启动了一个进程 - 在这种情况下,我认为它将启动您的浏览器。第二个参数是命令行参数。程序使用此信息,以便他们知道启动时的行为(隐藏,打开默认文档等)。无论如何它与Query字符串无关。如果你更喜欢使用Process.Start(),那么尝试这样的事情:

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:

如果你运行它,它将打开Internet Explorer并在搜索框上打开带有测试的谷歌。如果那是你的页面,你可以通过调用以下方式访问“q”:

echo $_GET["q"]) 

#4


0  

In my applications i used different method i.e using webClient i done it

在我的应用程序中,我使用了不同的方法,即使用webClient我完成了它

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

#1


1  

Try passing it with the url itself

尝试使用url本身传递它

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

#2


1  

you should put the key parameter as a query string :

你应该把key参数作为查询字符串:

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

#3


1  

I would suggest using the HttpWebRequestClass.

我建议使用HttpWebRequestClass。

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

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.

这样,您还可以将数据发布到页面,添加身份验证参数,cookie等 - 以防您可能需要它。

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.

我不确定这在您的特定设置中是否重要,通过查询字符串传递数据是不安全的。但如果安全性也是一个问题,我会通过SSL连接POST数据。

Update:

更新:

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

所以,如果你将数据发布到你的php页面,就像这样:

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:

您可以使用php页面中的以下代码检索它:

echo $_POST["data"]) 

Update 2:

更新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:

AFAIK,ProcessStartInfo / Process.Start()实际上启动了一个进程 - 在这种情况下,我认为它将启动您的浏览器。第二个参数是命令行参数。程序使用此信息,以便他们知道启动时的行为(隐藏,打开默认文档等)。无论如何它与Query字符串无关。如果你更喜欢使用Process.Start(),那么尝试这样的事情:

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:

如果你运行它,它将打开Internet Explorer并在搜索框上打开带有测试的谷歌。如果那是你的页面,你可以通过调用以下方式访问“q”:

echo $_GET["q"]) 

#4


0  

In my applications i used different method i.e using webClient i done it

在我的应用程序中,我使用了不同的方法,即使用webClient我完成了它

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