我有一个 Web 服务,其中包含如下方法
[WebMethod]
public string UploadFile(byte[] bytes, string file_name)
{
}
我想使用 HttpWebRequest 调用此 Web 服务方法,以便我可以流式传输文件而无需在内存中缓冲。怎么做...我尝试按如下方式调用它
HttpWebRequest hw = HttpWebRequest.Create("http://localhost/xxx/xxx.asmx/Upload") as HttpWebRequest;
hw.ContentType = "application/x-www-form-urlencoded";
hw.AllowWriteStreamBuffering = false;
hw.Method = "POST";
hw.UnsafeAuthenticatedConnectionSharing = true;
hw.UserAgent = "test type";
hw.Credentials = CredentialCache.DefaultCredentials;
FileInfo fi = new FileInfo("C:/Documents and Settings/Administrator/Desktop/a.txt");
string bytes = "bytes=";
byte[] by = UTF8Encoding.UTF8.GetBytes(bytes);
byte[] fn = UTF8Encoding.UTF8.GetBytes("&file_name=a.txt");
hw.ContentLength = by.Length+fi.Length+fn.Length;
using (Stream postStream = hw.GetRequestStream())
{
FileStream br = new FileStream("C:/Documents and Settings/Administrator/Desktop/a.txt", FileMode.Open, FileAccess.Read);
postStream.Write(by, 0, by.Length);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while (true)
{
bytesRead = br.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
postStream.Write(buffer, 0, bytesRead);
}
br.Close();
postStream.Write(fn, 0, fn.Length);
postStream.Write(ct, 0, ct.Length);
}
// HERE :
using (HttpWebResponse response = hw.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string sssdd = reader.ReadToEnd();
}
但它显示一条消息“远程服务器返回错误:(500)内部服务器错误。” 在执行 **// HERE : ** 标记行时。
顺便说一句:我使用的是 asp.net 2.0,我不允许使用代理类,因为我的要求是流式传输大文件的数据。提前致谢