据我了解,您有一个 ashx 处理程序,您可以将文件上传到该处理程序并对其进行测试。
我附上了一个示例测试,该测试假定使用 POST 请求文件附件的 ashx 处理程序。
[TestMethod]
public void TestCallUploadHandler()
{
const string FILE_PATH = "C:\\foo.txt";
const string FILE_NAME = "foo.txt";
string UPLOADER_URI =
string.Format("http://www.foobar.com/handler.ashx?filename={0}", FILE_NAME);
using (var stream = File.OpenRead(FILE_PATH))
{
var httpRequest = WebRequest.Create(UPLOADER_URI) as HttpWebRequest;
httpRequest.Method = "POST";
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(httpRequest.GetRequestStream());
var httpResponse = httpRequest.GetResponse();
StreamReader reader = new StreamReader(httpResponse.GetResponseStream());
var responseString = reader.ReadToEnd();
//Check the responsestring and see if all is ok
}
}
基本上你正在做的是为POST创建一个 WebRequest并将文件流附加到它的请求和文件名到它的查询字符串。