【问题标题】:invoking web service using httprequest使用 httprequest 调用 Web 服务
【发布时间】:2009-02-12 06:21:57
【问题描述】:

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

【问题讨论】:

    标签: asp.net web-services soap streaming


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        如果它说远程服务器返回了错误,那么您应该查看远程服务器以查看错误是什么。在服务器上的事件日志中查看您收到错误时发生的事件。

        【讨论】:

          【解决方案4】:

          删除这些行:

          postStream.Write(by, 0, by.Length);
          .
          .
          .
          postStream.Write(fn, 0, fn.Length);
          postStream.Write(ct, 0, ct.Length);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-04-09
            • 2013-10-23
            • 2016-06-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多