问题描述
我想通过使用WebClient.UploadFile而不是HttpWebRequest 来简化我的代码,但是最终我在服务器端获得了几十个字节的文件,损坏。任何想法的错误在哪里?
感谢使用HttpWebRequest(工作正常):
$ b
使用WebClient(服务器端损坏的文件): 服务器端是一个WCF服务: WebClient.UploadFile 以多部分/表格数据格式发送数据。你想用什么来使用HttpWebRequest的代码是 WebClient.UploadData 方法: 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): Using WebClient (corrupt file on server end): Server side is a WCF service: 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: 这篇关于HttpWebRequest的作品。 WebClient.UploadFile不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
$ 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;
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;
<$
$ Web $ {
$ Web $ {$ $ $ $ $ $ $ b void FileUpload(string fileName,string hash,Stream fileStream);
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 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;
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;
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "up/file/{fileName}/{hash}")]
void FileUpload(string fileName, string hash, Stream fileStream);
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);