我尝试在xampp
网页上显示使用C#HTML Post发送的值,这是我的代码:
private void sendHtmlData(UInt16 value) {
var postData = "Package" + value.ToString()
var data = Encoding.ASCII.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost/dashboard/");
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream()) {
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
}
<!doctype html>
<html>
<head></head>
<body>
<? echo htmlspecialchars($_GET["Package"]); ?>
</body>
</html>
为什么这不起作用?例如,如果我调用该功能,我的网页上就不会发生任何事情。
答案 0 :(得分:1)
有两个失败:
1-post数据必须采用 key = value 格式,但您只是将键与值连接,您错过了“=”,更改
enumerate
到
var postData = "Package" + value.ToString();
还建议对值进行urlencode,以防它有任何特殊字符:
var postData = "Package=" + value.ToString();
2 - 如果您将数据作为POST发送,则必须将其作为POST检索,而不是作为GET,更改
var postData = "Package=" + Uri.EscapeDataString(value.ToString());
到
<? echo htmlspecialchars($_GET["Package"]); ?>