传递字符串从c# Windows窗体应用程序到php网页

本文关键字:应用程序 php 网页 窗体 Windows 字符串 | 更新日期: 2023-09-27 18:11:45

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

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

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

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

但是它什么也不打印

传递字符串从c# Windows窗体应用程序到php网页

尝试传递url本身

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

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

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

我建议使用HttpWebRequestClass。

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

通过这种方式,您还可以将数据发布到您的页面,添加认证参数,cookie等-以防您可能需要它。

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

更新:

所以如果你像这样把数据POST到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!");

可以通过在PHP页面中使用以下代码来检索它:

echo $_POST["data"]) 
更新2:

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

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

如果你运行它,它将打开internet explorer,并在搜索框中打开带有test的google。如果那是你的页面,你可以通过调用

来访问"q"
echo $_GET["q"]) 

在我的应用程序中,我使用了不同的方法,即使用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);
 }