.net GetResponse获取响应流(ReadDone2)时出错:ReceiveFailure

时间:2018-10-31 06:38:58

标签: c# .net

    private string PostWebRequest(string postUrl, string paramData)
    {

        byte[] bytes = Encoding.UTF8.GetBytes(paramData);

        HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
        webReq.Method = "POST";
       webReq.ContentType = "application/json";
        webReq.ContentLength = bytes.Length;
        using (Stream newStream = webReq.GetRequestStream())
        {
            newStream.Write(bytes, 0, bytes.Length);
        }
        using (WebResponse res = webReq.GetResponse())
        {
            Stream responseStream = res.GetResponseStream();
            StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
            string str = streamReader.ReadToEnd();
            streamReader.Close();
            responseStream.Close();
            return str;
        }
    }

当我为contentType设置一个值时,它是失败的。像这样

  

异常:在System.Net.WebConnection.HandleError   (System.Net.WebExceptionStatus st,System.Exception e,System.String   其中)[0x00000]在<3acaa45225a54f778710eeee6eff26f77f>:0中   System.Net.WebConnection.ReadDone(System.IAsyncResult结果)   在<3acaa45225a54f778710eeee6eff26f77f>:0中的[0x00000]   System.Net.Sockets.SocketAsyncResult + <> c.b__27_0   (系统对象状态)在[0x00000]中   <3acaa45225a54f778710ee6eff26f77f>:0在   System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem   ()[0x00000]在:0处   System.Threading.ThreadPoolWorkQueue.Dispatch()[0x00000]在   :0处   System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()   [0x00000]在:0中   System.Net.WebConnection.HandleError(System.Net.WebExceptionStatus   st,System.Exception e,System.String其中)(在   <3acaa45225a54f778710ee6eff26f77f>:0)重新抛出为WebException:错误   获取响应流(ReadDone2):ReceiveFailure

如果我未将ContentType设置为一个值,它将响应成功。

2 个答案:

答案 0 :(得分:0)

欢迎来到

尝试以下

tbstatus

HttpObject类

 /// <summary>
    /// Post request 
    /// </summary>
    /// <param name="httpObject"></param>
    /// <param name="parameters"></param>
    /// <returns></returns>
    public string SendRequest(HttpObject httpObject, IDictionary<string, string> parameters)
    {

        var httpWebRequest = (HttpWebRequest)WebRequest.Create(httpObject.Url);
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = httpObject.Method.ToString();

        if (parameters != null)
            foreach (var p in parameters)
            {
                httpWebRequest.Headers.Add(p.Key, p.Value);
            }


        string result = null;

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            streamWriter.Write(httpObject.Json);
            streamWriter.Flush();
            streamWriter.Close();
        }
        try
        {
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
            }
        }
        catch (WebException wex)
        {
            using (var streamReader = new StreamReader(wex.Response.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
            }
        }

        return result;
    }

答案 1 :(得分:0)

ajax的默认内容类型是'application/x-www-form-urlencoded; charset=UTF-8'。 当您将其设置为"application/json";时。服务器不接受此请求,因为您不发送json,而是发送字节。

将数据发送到服务器时,请使用此内容类型。默认值为“ application / x-www-form-urlencoded; charset = UTF-8”,在大多数情况下都可以。如果您将内容类型显式传递给$ .ajax(),则该内容类型将始终发送到服务器(即使未发送任何数据)。从jQuery 1.6开始,您可以传递false来告诉jQuery不要设置任何内容类型标头。注意:W3C XMLHttpRequest规范规定该字符集始终为UTF-8。指定其他字符集不会强制浏览器更改编码。注意:对于跨域请求,将内容类型设置为application / x-www-form-urlencoded,multipart / form-data或text / plain之外的任何类型,都会触发浏览器向服务器发送预检OPTIONS请求。