代码之家  ›  专栏  ›  技术社区  ›  Brian Sweeney

无法发送具有此谓词类型的内容正文

  •  83
  • Brian Sweeney  · 技术社区  · 15 年前

    谢谢,

    //run get and grab response
    WebRequest request = WebRequest.Create(get.AbsoluteUri + args);
    request.Method = "GET";
    Stream stream = request.GetRequestStream();           // <= explodes here
    XmlTextReader reader = new XmlTextReader(stream);
    
    4 回复  |  直到 10 年前
        1
  •  148
  •   Community CDub    8 年前

    不要简单地获取请求流。获取请求不 通常 有尸体(即使是 not technically prohibited by HTTP WebRequest 不支持它-但这就是所谓的 GetRequestStream

    既然你想 阅读 在我看来,你真的想 并从中读取响应流:

    WebRequest request = WebRequest.Create(get.AbsoluteUri + args);
    request.Method = "GET";
    using (WebResponse response = request.GetResponse())
    {
        using (Stream stream = response.GetResponseStream())
        {
            XmlTextReader reader = new XmlTextReader(stream);
            ...
        }
    }
    
        2
  •  3
  •   flam3    6 年前

    我在使用Flurl.Http时也遇到了类似的问题:

    Flurl.Http.FlurlHttpException: Call failed. Cannot send a content-body with this verb-type. GET http://******:8301/api/v1/agents/**** ---> System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.

    问题是我用了 .WithHeader("Content-Type", "application/json") 创建IFlurlRequest时。

        3
  •  2
  •   Reactgular    11 年前

    因为你没有指定标题。

    我添加了一个扩展示例:

    var request = (HttpWebRequest)WebRequest.Create(strServer + strURL.Split('&')[1].ToString());
    

    方法头:

    private void Header(ref HttpWebRequest p_request, string p_Method)
    {
        p_request.ContentType = "application/x-www-form-urlencoded";
        p_request.Method = p_Method;
        p_request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows CE)";
        p_request.Host = strServer.Split('/')[2].ToString();
        p_request.Accept = "*/*";
        if (String.IsNullOrEmpty(strURLReferer))
        {
            p_request.Referer = strServer;
        }
        else
        {
            p_request.Referer = strURLReferer;
        }
        p_request.Headers.Add("Accept-Language", "en-us\r\n");
        p_request.Headers.Add("UA-CPU", "x86 \r\n");
        p_request.Headers.Add("Cache-Control", "no-cache\r\n");
        p_request.KeepAlive = true;
    }
    
        4
  •  -5
  •   Ankur    12 年前

    请在读取响应流之前设置请求内容类型;

     request.ContentType = "text/xml";
    
    推荐文章