我试图通过C#从我的网络服务器读取和写入文件。
到目前为止,通过PHP我开始工作,你可以用file_put_contents()
写入文件。保存的文件是文本文件,因此您可以轻松阅读它们。我的C#程序响应正常,我得到了我想要的值。
private string GetWarnInfo(string id)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"http://example.com/{id}.txt");
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
return reader.ReadToEnd();
}
}
catch(Exception)
{
return null;
}
}
100%的时间没有返回null,这是成功的。
好吧,写作。我在example.php
中的PHP看起来像这样:
if (file_exists($_GET['id'] . '.txt'))
{
unlink($_GET['id'] . '.txt');
file_put_contents($_GET['id'] . '.txt', $_GET['info']);
} else {
file_put_contents($_GET['id'] . '.txt', $_GET['info']);
}
虽然它通过浏览器调用(http://example.com/example.php?id=23&info=w3
)完全有效,并且实际上是文本文件,但我无法使用C#:
public void ServerRequest(string id, string info)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://example.com/example.php?id=" + id + "&info=" + info);
request.Method = "GET";
}
例如,我调用了ServerRequest((23).ToString(), "w3")
,但文件不会更改,它将始终为不存在或处于最后状态(如果存在)。
什么可能导致这个问题?我该如何解决?
答案 0 :(得分:0)
感谢@Barmar
我已经找到了问题,如果我不打电话给map[dlvl]
,它将永远不会启动网络请求。完成后,一切正常!