【发布时间】:2014-06-04 18:11:00
【问题描述】:
我是 C# 新手,上传文件到 Web 服务器时遇到问题。
我在生成代码时使用了this 作为模板。
try
{
using (WebClient client = new WebClient()) {
client.Headers.Add("Content-Type", "binary/octet-stream");
ans = client.UploadFile(remoteSiteFileURL, "POST", @"D:\test.txt");
}
string responseAsString = Encoding.Default.GetString(ans);
MessageBox.Show("Done" + responseAsString);
}
当我调用此代码时,我什么也得不到,只是在某个时间(可能是一分钟)'The operation has time out' 后出现异常。
文件 test.txt 小于 1kb(实际上是几个字节)。 Web 服务(站点)工作正常,我轻松上传了 500kb 的数据。
我在测试上传时使用了下一个 html 表单:
<h1>Upload file form</h1>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
Send file<input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
没有授权,在upload.php中设置了会话控制。
更新1: 1)当我运行此代码时,我无法关闭我的应用程序。它挂它。 2)我关闭了防火墙,这没有帮助。
更新2: 我在“upload.php”放置了一个记录器,它在被调用时写入文件。所以,我的日志中没有任何内容,即它没有被调用(我不确定。可能会在传输完整文件时调用它(脚本))。
问题可能是什么?我错过了什么吗?
【问题讨论】:
标签: c#