從我的理解,你有一個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