HttpWebRequest的作品。 WebClient.UploadFile不 [英] HttpWebRequest works. WebClient.UploadFile doesn't

查看:175
本文介绍了HttpWebRequest的作品。 WebClient.UploadFile不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用WebClient.UploadFile而不是HttpWebRequest 来简化我的代码,但是最终我在服务器端获得了几十个字节的文件,损坏。任何想法的错误在哪里?

感谢使用HttpWebRequest(工作正常):
$ b


$ b $ pre $ HttpWebRequest req b ConnectionManager.FileServerAddress +:+
ConnectionManager.FileServerPort +
/ binary / up /+ category +/+
Path.GetFileName(filename)+/+ safehash);

req.Method =POST;
req.ContentType =binary / octet-stream;
req.AllowWriteStreamBuffering = false;
req.ContentLength = bytes.Length;
Stream reqStream = req.GetRequestStream();

int offset = 0;
while(offset< ____)
{
reqStream.Write(bytes,offset,_________);
_______
_______
_______

}
reqStream.Close();

尝试
{
HttpWebResponse resp =(HttpWebResponse)req.GetResponse();

catch(例外e)
{
_____________
}
返回safehash;

使用WebClient(服务器端损坏的文件):

  var client = new WebClient(); 
client.Encoding = Encoding.UTF8;
client.Headers.Add(HttpRequestHeader.ContentType,binary / octet-stream);

client.UploadFile(new Uri(http://+
ConnectionManager.FileServerAddress +:+
ConnectionManager.FileServerPort +
/ binary / /+ category +/+
Path.GetFileName(filename)+/+ safehash),filename);

返回safehash;

服务器端是一个WCF服务:

<$
$ Web $ {
$ Web $ {$ $ $ $ $ $ $ b void FileUpload(string fileName,string hash,Stream fileStream);


解决方案

WebClient.UploadFile 以多部分/表格数据格式发送数据。你想用什么来使用HttpWebRequest的代码是 WebClient.UploadData 方法:

  var client = new WebClient(); 
client.Encoding = Encoding.UTF8;
client.Headers [HttpRequestHeader.ContentType] =application / octet-stream;
byte [] fileContents = File.ReadAllBytes(filename);
client.UploadData(new Uri(http://+ ConnectionManager.FileServerAddress +:+
ConnectionManager.FileServerPort +
/ binary / up /+ category +/ +
Path.GetFileName(filename)+/+ safehash),fileContents);


I thought I figured out a way to simplify my code by using WebClient.UploadFile instead of HttpWebRequest, but I end up getting a file on the server end that is a few dozen bytes too short and corrupted. Any idea where the bug lies?

Thanks

Using HttpWebRequest (works fine):

       HttpWebRequest req = (HttpWebRequest)HttpWebRequest
                                 .Create("http://" +
                                  ConnectionManager.FileServerAddress + ":" +
                                  ConnectionManager.FileServerPort +
                                  "/binary/up/" + category + "/" +  
                                  Path.GetFileName(filename) + "/" + safehash);

        req.Method = "POST";
        req.ContentType = "binary/octet-stream";
        req.AllowWriteStreamBuffering = false;
        req.ContentLength = bytes.Length;
        Stream reqStream = req.GetRequestStream();

        int offset = 0;
        while (offset < ____)
        {
            reqStream.Write(bytes, offset, _________);
             _______
             _______
             _______

        }
        reqStream.Close();

        try
        {
            HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
        }
        catch (Exception e)
        {
            _____________
        }
        return safehash;

Using WebClient (corrupt file on server end):

      var client = new WebClient();
      client.Encoding = Encoding.UTF8;
      client.Headers.Add(HttpRequestHeader.ContentType, "binary/octet-stream");

      client.UploadFile(new Uri("http://" +
              ConnectionManager.FileServerAddress + ":" +
              ConnectionManager.FileServerPort +
              "/binary/up/" + category + "/" +
              Path.GetFileName(filename) + "/" + safehash), filename);

      return safehash;

Server side is a WCF service:

  [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "up/file/{fileName}/{hash}")]

    void FileUpload(string fileName, string hash, Stream fileStream);

解决方案

WebClient.UploadFile sends the data in a multipart/form-data format. What you want to use to have the equivalent to the code using HttpWebRequest is the WebClient.UploadData method:

var client = new WebClient();
client.Encoding = Encoding.UTF8;
client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
byte[] fileContents = File.ReadAllBytes(filename);
client.UploadData(new Uri("http://" + ConnectionManager.FileServerAddress + ":" +
       ConnectionManager.FileServerPort +
       "/binary/up/" + category + "/" +
       Path.GetFileName(filename) + "/" + safehash), fileContents);

这篇关于HttpWebRequest的作品。 WebClient.UploadFile不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆