从我的理解,你有一个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和FILESTREAM连接到其请求和文件名,它的查询字符串。
辉煌的感谢。怎么样增加安全性? – Rob 2012-08-14 15:44:14
你在说什么样的安全性? – 2012-08-14 15:46:54
对不起,它需要用户名和密码。有人在处理程序中编写了代码,因此它需要context.Request.Headers [“授权”] – Rob 2012-08-14 15:49:28