1

我有一个 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,我不允许使用代理类,因为我的要求是流式传输大文件的数据。提前致谢

4

4 回答 4

1

自从我弄乱这些东西以来已经有一段时间了,但我认为你可以在代理类中获得底层的 HttpRequest 。可能比编写自己的 SOAP 到 POST 容易得多。

于 2009-08-11T16:46:45.893 回答
1

在您的浏览器中打开http://localhost/xxx/xxx.asmx/Upload,ASP.NET 将为您提供一个示例 POST 请求,您需要发送以调用该方法。从那里开始。

于 2009-02-12T06:33:22.730 回答
0

If it says that the remote server returned an error, then you should look on the remote server to see what the error was. Look in the Event Log on the server for events at around the time you received the error.

于 2009-02-14T19:03:27.247 回答
0

删除这些行:

postStream.Write(by, 0, by.Length);
.
.
.
postStream.Write(fn, 0, fn.Length);
postStream.Write(ct, 0, ct.Length);
于 2012-09-20T08:19:49.383 回答