.NET POST 到 PHP 页面
我正在尝试从我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设 sData 的值是(比如说)“10.1.1.1”,那么您当前一开始就没有发布正确的表单数据。变量的名称不是由
您编写的文本的一部分,您需要执行以下操作:
然后在 PHP 中使用
ipaddress
表单参数。另请注意,您应该给出二进制内容长度,该长度可能与字符串字符长度不同。当然,如果这里的字符串完全是 ASCII 也没关系,如果它是 IP 地址,我希望是这样……但对于其他用途,值得记住。 (同样,您通常需要记住任何需要特殊编码的字符。)
另请注意,最好对
StreamWriter
、使用
等,以确保即使抛出异常,所有内容也会关闭。using
语句。 HttpResponseAssuming 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 byYou need to do something like:
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 theStreamWriter
,HttpResponse
etc, to make sure that everything gets closed even if an exception is thrown.