HttpRequest and POST

前端 未结 2 901
星月不相逢
星月不相逢 2021-01-14 17:34

I keep getting one of the following error messages :

\"The remote server returned an error: (400) Bad Request.\"  
               OR
\"System.Net.ProtocolVio         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-14 17:43

        req.ContentLength = bld.Length;
        StreamWriter writer = new StreamWriter(req.GetRequestStream());
        var encodedData = Encoding.ASCII.GetBytes(bld.ToString());
        writer.Write(encodedData);
    

    You are not writing what you say you are going to be writing- you write the ASCII encoded bytes not your original byte array - the ContentLength has to match the number of bytes you write. Instead do:

        var encodedData = Encoding.ASCII.GetBytes(bld.ToString());
        req.ContentLength = encodedData.Length;
    

提交回复
热议问题