[英]C# Uploading file to HTTPServer
我需要编写一个httpclient,以使用http将巨大的文件上传到服务器。 编码
public Stream function()
{
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
request.Method = "POST";
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
return dataStream;
}
然后,我将dataStream返回给此函数的调用方,以便应用程序继续使用write写入流。 然后,一旦编写完成,将调用GetResponse,然后关闭流。 但我得到了例外
写入流时,System.Net.HttpWriteStream.Write()处的ProtocolViolationException。
请帮助。
由于请求方法是GET或HEAD,可能会引发ProtocolViolationException
。
您必须将Method设置为POST,如下所示:
// Set the 'Method' property of the 'Webrequest' to 'POST'.
request.Method = "POST";
请参阅MSDN上的HttpWebRequest.GetRequestStream方法文档。
您是否考虑过在使用浏览器时使用Fiddler调试过程?
这是一个简单的代理,可让您检查HTTP流量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.