본문 바로가기

C#

HttpWebRequest 클래스를 활용한 POST 전송

728x90
var result = Encoding.UTF8.GetBytes(postParams);
var request = WebRequest.Create(requestUrl);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.ContentLength = result.Length;
request.Timeout = Constants.CrmMessage.TimeOut;

var postDataStream = request.GetRequestStream();
postDataStream.Write(result, 0, result.Length);
postDataStream.Close();

var response = request.GetResponse();
var dataStream = response.GetResponseStream();
if (dataStream == null) return string.Empty;

var reader = new StreamReader(dataStream);
return reader.ReadToEnd();

[출처]
https://jp1020.tistory.com/entry/HttpWebRequest-%ED%81%B4%EB%9E%98%EC%8A%A4%EB%A5%BC-%ED%99%9C%EC%9A%A9%ED%95%9C-POST-%EC%A0%84%EC%86%A1

728x90