C# 使用httpwebrequest发送大文件:将请求URI发送到long

C# 使用httpwebrequest发送大文件:将请求URI发送到long,c#,httpwebrequest,httpwebresponse,C#,Httpwebrequest,Httpwebresponse,我制作了一个包装器,用于向Web服务器发送和接收数据。到目前为止,它还可以工作,但当我尝试使用它发送包含文件字节数组(pdf或word文档)的内容时,它失败了,并将消息请求URI发送到long 这是包装器的代码: public class CallWebService { public enum Methods { GET = 1, POST = 2, PUT = 3, DELETE = 4 }

我制作了一个包装器,用于向Web服务器发送和接收数据。到目前为止,它还可以工作,但当我尝试使用它发送包含文件字节数组(pdf或word文档)的内容时,它失败了,并将消息请求URI发送到long

这是包装器的代码:

  public class CallWebService
{
    public enum Methods
    {
        GET = 1,
        POST = 2,
        PUT = 3,
        DELETE = 4
    }

    //public TResult ExecutionResult { get; set; }
    public string WebServiceURL { get; set; }
    public Methods Method { get; set; }

    public CallWebService(string sWebServiceUrl)
    {
        this.WebServiceURL = sWebServiceUrl;
    }

    public TResult Execute<TResult>(string sCommand, Methods mMethod)
    {
        return Execute<TResult, string>(sCommand, mMethod, null);
    }

    public TResult Execute<TResult, TParam>(string sCommand, Methods mMethod, TParam parameter)
    {
        TResult executionResult = default(TResult);
        string requestUriString = "";
        try
        {
            if (this.WebServiceURL != null)
            {
                requestUriString = Path.Combine(this.WebServiceURL, sCommand); 

                JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
                jsonSerializer.MaxJsonLength = int.MaxValue;
                string jsonParameter = null;
                if (parameter != null)
                {
                    jsonParameter = jsonSerializer.Serialize(parameter);
                    requestUriString += "?" + jsonParameter;
                }

                HttpWebRequest req = WebRequest.Create(requestUriString) as HttpWebRequest;
                req.KeepAlive = false;
                req.Method = mMethod.ToString().ToUpper();

                Stream dataStream;

                if (("POST,PUT").Split(',').Contains(mMethod.ToString().ToUpper()))
                {
                    if (jsonParameter != null && jsonParameter.ToString().Length > 0)
                    {
                        byte[] byteArray = Encoding.UTF8.GetBytes(jsonParameter);
                        // Set the ContentType property of the WebRequest.
                        req.ContentType = "application/json; charset=utf-8";
                        // Set the ContentLength property of the WebRequest.
                        req.ContentLength = byteArray.Length;
                        // Get the request stream.
                        dataStream = req.GetRequestStream();
                        // Write the data to the request stream.
                        dataStream.Write(byteArray, 0, byteArray.Length);
                        // Close the Stream object.
                        dataStream.Close();
                    }
                }
                // Get the response.
                HttpWebResponse response = req.GetResponse() as HttpWebResponse;
                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.
                string responseFromServer = reader.ReadToEnd();
                // Clean up the streams.
                reader.Close();
                dataStream.Close();
                response.Close();
                executionResult = jsonSerializer.Deserialize<TResult>(responseFromServer);
            }
            else
            {
                throw new Exception("WebServiceURL not provided. Please initialize the object first.");
            }
        }
        catch (Exception ex)
        {
          //To Do
        }
        return executionResult;
    }
}
公共类CallWebService
{
公共枚举方法
{
GET=1,
员额=2,
PUT=3,
删除=4
}
//public TResult ExecutionResult{get;set;}
公共字符串WebServiceURL{get;set;}
公共方法方法{get;set;}
公共CallWebService(字符串sWebServiceUrl)
{
this.WebServiceURL=sWebServiceUrl;
}
公共TResult Execute(字符串sCommand、方法mMethod)
{
返回Execute(sCommand、mMethod、null);
}
公共TResult Execute(字符串sCommand、方法mMethod、TParam参数)
{
TResult executionResult=默认值(TResult);
字符串requestUriString=“”;
尝试
{
if(this.WebServiceURL!=null)
{
requestUriString=Path.Combine(this.WebServiceURL,sCommand);
JavaScriptSerializer jsonSerializer=新的JavaScriptSerializer();
jsonSerializer.MaxJsonLength=int.MaxValue;
字符串jsonParameter=null;
if(参数!=null)
{
jsonParameter=jsonSerializer.Serialize(参数);
requestUriString+=“?”+jsonParameter;
}
HttpWebRequest req=WebRequest.Create(requestUriString)为HttpWebRequest;
req.KeepAlive=false;
req.Method=mMethod.ToString().ToUpper();
流数据流;
if(((“POST,PUT”).Split(“,”).Contains(mMethod.ToString().ToUpper()))
{
if(jsonParameter!=null&&jsonParameter.ToString().Length>0)
{
byte[]byteArray=Encoding.UTF8.GetBytes(jsonParameter);
//设置WebRequest的ContentType属性。
req.ContentType=“application/json;charset=utf-8”;
//设置WebRequest的ContentLength属性。
req.ContentLength=字节数组长度;
//获取请求流。
dataStream=req.GetRequestStream();
//将数据写入请求流。
写入(byteArray,0,byteArray.Length);
//关闭流对象。
dataStream.Close();
}
}
//得到回应。
HttpWebResponse=req.GetResponse()作为HttpWebResponse;
//获取包含服务器返回的内容的流。
dataStream=response.GetResponseStream();
//使用StreamReader打开流以便于访问。
StreamReader=新的StreamReader(数据流);
//阅读内容。
字符串responseFromServer=reader.ReadToEnd();
//清理溪流。
reader.Close();
dataStream.Close();
response.Close();
executionResult=jsonSerializer.Deserialize(responseFromServer);
}
其他的
{
抛出新异常(“未提供WebServiceURL。请先初始化对象。”);
}
}
捕获(例外情况除外)
{
//做
}
返回执行结果;
}
}
}


感谢您提供的任何帮助

看起来您总是将参数附加到查询字符串上,然后(对于POST和PUT)也将其放入请求正文中。通常,它应该只放在GET的查询字符串中。此外,你几乎应该总是在你的问题上加上一个语言标签。看起来很有可能,但你不应该强迫潜在的帮助者a)猜测,或b)甚至看不到你的问题。现场加油..谢谢鹰眼!谢谢你的语言评论!