【问题标题】:How read binary response from ServerXMLHTTP?如何从 ServerXMLHTTP 读取二进制响应?
【发布时间】:2014-01-11 12:51:32
【问题描述】:

我正在使用 ServerXMLHTTP 和 HTTPRequester 插件发送请求,我可以看到我的请求的响应是请求文件的原始二进制数据。 我需要读取此二进制数据并将其保存在 Byte[] 中,但我不断收到错误消息: 当前编码处于无效状态的文件末尾。

这是我的代码:

 ServerXMLHTTP HTTPRequest=new ServerXMLHTTP();
 HTTPRequest.open("POST",@"https://thetargetwebsite/",false);
 HTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 HTTPRequest.send("file_id=" + file_id);
string binaryfile=HTTPRequest.responseText; // causes the error
//OR
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] bytes = encoding.GetBytes(HTTPRequest.responseText); // also causes the error

你能帮我看看如何用二进制数据读取响应文本吗?

编辑: 如果我不使用 ServerXMLHTTP,我应该使用什么来代替?

【问题讨论】:

    标签: encoding binary response serverxmlhttp


    【解决方案1】:

    我改用这个,效果很好:

             byte[] result;
             byte[] buffer = new byte[4096];
    
             System.Net.WebRequest wr = System.Net.WebRequest.Create(url);
    
             using (System.Net.WebResponse response = wr.GetResponse())
             {
                 using (Stream responseStream = response.GetResponseStream())
                 {
                     using (MemoryStream memoryStream = new MemoryStream())
                     {
                         int count = 0;
                         do
                         {
                             count = responseStream.Read(buffer, 0, buffer.Length);
                             memoryStream.Write(buffer, 0, count);
    
                         } while (count != 0);
    
                         result = memoryStream.ToArray();
    
                     }
                 }
             }
    

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      • 2020-05-09
      • 2021-03-04
      • 2018-09-24
      • 1970-01-01
      • 2019-04-10
      相关资源
      最近更新 更多