.NET POST 到 PHP 页面

发布于 2024-11-13 22:21:27 字数 1405 浏览 3 评论 0原文

我正在尝试从我的 C# .NET 应用程序实现一个非常基本的系统,该系统将计算机的 IP 地址发送到我的 Web 服务器上的authenticate.php。 PHP 页面将根据数据库检查此 IP 地址,并返回“是”或“否”。

自从我使用 PHP 以来已经很长时间了,我有点困惑。这是我的 .NET 函数的样子。

public static bool IsAuthenticated()
{
    string sData = getPublicIP();
    Uri uri = new Uri("http://www.mysite.com/authenticate.php");
    if (uri.Scheme == Uri.UriSchemeHttp)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = WebRequestMethods.Http.Post;
        request.ContentLength = sData.Length;
        request.ContentType = "application/x-www-form-urlencoded";

        // POST the data to the authentication page
        StreamWriter writer = new StreamWriter(request.GetRequestStream());
        writer.Write(sData);
        writer.Close();

        // Retrieve response from authentication page
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string sResponse = reader.ReadToEnd();
        response.Close();

        if (sResponse == "yes")
        {
            Console.WriteLine("Authentication was Successful.");
            return true;
        }
        else
        {
            Console.WriteLine("Authentication Failed!");
            return false;
        }
    }
}

那么 POST 变量将是 $_POST['sData'];我如何回复我的申请并提供结果?

I am trying to implement a very basic system from my C# .NET application that sends the IP address of the machine to authenticate.php on my web server. The php page will check this IP address against a database and either respond back with "yes" or "no".

It has been a long time since I worked with PHP, and I am a little bit confused. Here is what my .NET function looks like.

public static bool IsAuthenticated()
{
    string sData = getPublicIP();
    Uri uri = new Uri("http://www.mysite.com/authenticate.php");
    if (uri.Scheme == Uri.UriSchemeHttp)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = WebRequestMethods.Http.Post;
        request.ContentLength = sData.Length;
        request.ContentType = "application/x-www-form-urlencoded";

        // POST the data to the authentication page
        StreamWriter writer = new StreamWriter(request.GetRequestStream());
        writer.Write(sData);
        writer.Close();

        // Retrieve response from authentication page
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string sResponse = reader.ReadToEnd();
        response.Close();

        if (sResponse == "yes")
        {
            Console.WriteLine("Authentication was Successful.");
            return true;
        }
        else
        {
            Console.WriteLine("Authentication Failed!");
            return false;
        }
    }
}

So would the POST variable be $_POST['sData']; and how do I respond back to my application with the result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神仙妹妹 2024-11-20 22:21:27

假设 sData 的值是(比如说)“10.1.1.1”,那么您当前一开始就没有发布正确的表单数据。变量的名称不是由

 writer.Write(sData);

您编写的文本的一部分,您需要执行以下操作:

 string postData = "ipaddress=" + sData;

然后在 PHP 中使用 ipaddress 表单参数。

另请注意,您应该给出二进制内容长度,该长度可能与字符串字符长度不同。当然,如果这里的字符串完全是 ASCII 也没关系,如果它是 IP 地址,我希望是这样……但对于其他用途,值得记住。 (同样,您通常需要记住任何需要特殊编码的字符。)

另请注意,最好对 StreamWriter使用 using 语句。 HttpResponse 等,以确保即使抛出异常,所有内容也会关闭。

Assuming the value of sData is (say) "10.1.1.1" then you're currently not posting proper form data in the first place. The name of the variable isn't part of the text written by

 writer.Write(sData);

You need to do something like:

 string postData = "ipaddress=" + sData;

and then use the ipaddress form parameter within your PHP.

Note also that you should be giving the binary content length, which may not be the same as the string length in characters. Of course it's okay if the string here is entirely ASCII, which I'd expect if it's an IP address... but it's worth bearing in mind for other uses. (Likewise you would normally need to bear in mind any characters which need special encoding.)

Also note that it would be better to use using statements for the StreamWriter, HttpResponse etc, to make sure that everything gets closed even if an exception is thrown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文