如何使用c#从打开的网页中获取字段数据

本文关键字:网页 获取 字段 数据 何使用 | 更新日期: 2023-09-27 18:11:58

服务器端是php网站,客户端是c# winform。

是否有任何方法可以使用c#获得PHP当前页面的字段值?

例如,在php中我有客户的firstName字段(他刚刚键入的),我需要在c#程序中使用这个值。

提前感谢!

如何使用c#从打开的网页中获取字段数据

假设你有一个php脚本,其中你获得了firstName(我们设为getname。php):

<?php
$firstName = $_POST['firstName']; // or any other way to get it
$arr = array('firstname' => $firstName);
echo json_encode($arr);
?>

将显示如下内容:

{"firstname": your_firstname }

所以现在这个JSON可以在任何你想要的编程语言中使用。对于c#:

在System中使用WebClient类净:

var json = new WebClient().DownloadString("url_of_php_file");

请记住,WebClient是IDisposable,所以您可能会在生产代码中添加using语句。这看起来像:

using (WebClient wc = new WebClient())
{
   var json = wc.DownloadString("url_of_php_file");
}