반응형
WebRequest의 본문 데이터 설정
ASP.NET에서 웹 요청을 만들고 있는데 본문에 많은 데이터를 추가해야합니다. 어떻게합니까?
var request = HttpWebRequest.Create(targetURL);
request.Method = "PUT";
response = (HttpWebResponse)request.GetResponse();
와 HttpWebRequest.GetRequestStream
http://msdn.microsoft.com/en-us/library/d4cek6cc.aspx의 코드 예제
string postData = "firstone=" + inputData;
ASCIIEncoding encoding = new ASCIIEncoding ();
byte[] byte1 = encoding.GetBytes (postData);
// Set the content type of the data being posted.
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
// Set the content length of the string being posted.
myHttpWebRequest.ContentLength = byte1.Length;
Stream newStream = myHttpWebRequest.GetRequestStream ();
newStream.Write (byte1, 0, byte1.Length);
내 코드 중 하나에서 :
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Credentials = this.credentials;
request.Method = method;
request.ContentType = "application/atom+xml;type=entry";
using (Stream requestStream = request.GetRequestStream())
using (var xmlWriter = XmlWriter.Create(requestStream, new XmlWriterSettings() { Indent = true, NewLineHandling = NewLineHandling.Entitize, }))
{
cmisAtomEntry.WriteXml(xmlWriter);
}
try
{
return (HttpWebResponse)request.GetResponse();
}
catch (WebException wex)
{
var httpResponse = wex.Response as HttpWebResponse;
if (httpResponse != null)
{
throw new ApplicationException(string.Format(
"Remote server call {0} {1} resulted in a http error {2} {3}.",
method,
uri,
httpResponse.StatusCode,
httpResponse.StatusDescription), wex);
}
else
{
throw new ApplicationException(string.Format(
"Remote server call {0} {1} resulted in an error.",
method,
uri), wex);
}
}
catch (Exception)
{
throw;
}
이것은 도움이 될 것입니다 :
var request = (HttpWebRequest)WebRequest.Create("http://example.com/page.asp");
string stringData = ""; //place body here
var data = Encoding.ASCII.GetBytes(stringData); // or UTF8
request.Method = "PUT";
request.ContentType = ""; //place MIME type here
request.ContentLength = data.Length;
var newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
참고 URL : https://stackoverflow.com/questions/4256136/setting-a-webrequests-body-data
반응형
'Programing' 카테고리의 다른 글
html 웹 페이지에서 모든 요소의 글꼴 속성을 지정하는 방법은 무엇입니까? (0) | 2020.07.25 |
---|---|
최단 경로를 찾을 때 너비 우선 검색은 어떻게 작동합니까? (0) | 2020.07.24 |
장고. (0) | 2020.07.24 |
Rstudio에서 작업 디렉토리를 소스 파일 위치로 설정하기위한 R 명령 (0) | 2020.07.24 |
console.writeline 및 System.out.println (0) | 2020.07.24 |