我在c#application
中使用了以下代码string verification_url = @"http://site.com/posttest.php?";
string verification_data = "test=524001A";
string result = string.Empty;
result = Post(verification_url, verification_data);
public string Post(string url, string data)
{
string result = "";
try
{
byte[] buffer = Encoding.GetEncoding(1252).GetBytes(data);
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(@url);
WebReq.Method = "POST";
WebReq.ContentType = "application/x-www-form-urlencoded";
WebReq.ContentLength = buffer.Length;
Stream PostData = WebReq.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
result = _Answer.ReadToEnd();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if (result.Length < 0)
result = "";
return result;
}
服务器端PHP代码
<?php
$test=$_REQUEST['test'];
echo $test;
?>
post方法总是返回空值
请帮帮我
答案 0 :(得分:1)
尝试
<?php
print_r($_REQUEST);
?>
显示原始REQUEST变量。我不知道你在c#代码中设置测试的位置。
答案 1 :(得分:0)
您需要将验证数据作为参数添加到请求网址,例如
string verification_data = "test=524001A";
string verification_url = @"http://site.com/posttest.php?" + verification_data;
这样您的实际请求网址就是:
http://site.com/posttest.php?test=524001A
答案 2 :(得分:0)
您可以尝试不关闭RequestStream吗?请尝试删除以下行:
PostData.Close();
PS:你有必要的.net权限吗?
答案 3 :(得分:0)
我更改了以下内容
string verification_url = @"http://site.com/posttest.php?";
到
string verification_url = @"http://www.site.com/posttest.php?";
它现在可以正常使用