How to set the content of an HttpWebRequest in C#?

后端 未结 5 1715
余生分开走
余生分开走 2020-12-03 09:47

An HttpWebRequest has the properties ContentLength and ContentType, but how do you actually set the content of the request?

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 10:22

    HttpWebRequest.GetRequestStream() gets the request Stream. After you have set the headers, use GetRequestStream() and write the content to the stream.

    This post explains how to transmit files using HttpWebRequest, which should provide a good example of how to send content.

    But, basically the format would be

     var stream = request.GetRequestStream();
     stream.Write( stuff );
     stream.Close();
     var response = request.GetResponse();
    

提交回复
热议问题