HttpWebRequest를 사용하여 양식 데이터 게시
내 웹 애플리케이션에없는 지정된 URL에 일부 양식 데이터를 게시하고 싶습니다. "domain.client.nl"과 같은 동일한 도메인이 있습니다. 웹 응용 프로그램에는 "web.domain.client.nl"이라는 URL이 있으며 게시 할 URL은 "idp.domain.client.nl"입니다. 하지만 내 코드는 아무것도하지 않습니다 ..... 누군가 내가 뭘 잘못하고 있는지 압니까?
Wouter
StringBuilder postData = new StringBuilder();
postData.Append(HttpUtility.UrlEncode(String.Format("username={0}&", uname)));
postData.Append(HttpUtility.UrlEncode(String.Format("password={0}&", pword)));
postData.Append(HttpUtility.UrlEncode(String.Format("url_success={0}&", urlSuccess)));
postData.Append(HttpUtility.UrlEncode(String.Format("url_failed={0}", urlFailed)));
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] postBytes = ascii.GetBytes(postData.ToString());
// set up request object
HttpWebRequest request;
try
{
request = (HttpWebRequest)HttpWebRequest.Create(WebSiteConstants.UrlIdp);
}
catch (UriFormatException)
{
request = null;
}
if (request == null)
throw new ApplicationException("Invalid URL: " + WebSiteConstants.UrlIdp);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
// add post data to request
Stream postStream = request.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Flush();
postStream.Close();
필드 이름과 값은 모두 URL로 인코딩되어야합니다. 게시물 데이터와 쿼리 문자열의 형식이 동일합니다.
.net 방식은 다음과 같습니다.
NameValueCollection outgoingQueryString = HttpUtility.ParseQueryString(String.Empty);
outgoingQueryString.Add("field1","value1");
outgoingQueryString.Add("field2", "value2");
string postdata = outgoingQueryString.ToString();
이것은 필드와 값 이름의 인코딩을 처리합니다.
이 시도:
var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");
var postData = "thing1=hello";
postData += "&thing2=world";
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
양식을 잘못 인코딩하고 있습니다. 다음 값만 인코딩해야합니다.
StringBuilder postData = new StringBuilder();
postData.Append("username=" + HttpUtility.UrlEncode(uname) + "&");
postData.Append("password=" + HttpUtility.UrlEncode(pword) + "&");
postData.Append("url_success=" + HttpUtility.UrlEncode(urlSuccess) + "&");
postData.Append("url_failed=" + HttpUtility.UrlEncode(urlFailed));
편집하다
나는 틀렸다. 따르면 RFC1866 섹션 8.2.1 모두 이름과 값은 인코딩되어야한다.
But for the given example, the names do not have any characters that needs to be encoded, so in this case my code example is correct ;)
The code in the question is still incorrect as it would encode the equal sign which is the reason to why the web server cannot decode it.
A more proper way would have been:
StringBuilder postData = new StringBuilder();
postData.AppendUrlEncoded("username", uname);
postData.AppendUrlEncoded("password", pword);
postData.AppendUrlEncoded("url_success", urlSuccess);
postData.AppendUrlEncoded("url_failed", urlFailed);
//in an extension class
public static void AppendUrlEncoded(this StringBuilder sb, string name, string value)
{
if (sb.Length != 0)
sb.Append("&");
sb.Append(HttpUtility.UrlEncode(name));
sb.Append("=");
sb.Append(HttpUtility.UrlEncode(value));
}
참고URL : https://stackoverflow.com/questions/14702902/post-form-data-using-httpwebrequest
'development' 카테고리의 다른 글
finally 블록을 사용하는 이유는 무엇입니까? (0) | 2020.09.24 |
---|---|
JSON 키에 인용 문자열을 사용하는 실용적인 이유가 있습니까? (0) | 2020.09.24 |
VS2012 편집기 탭 색상을 끄는 방법은 무엇입니까? (0) | 2020.09.24 |
SQL Server 2008 Management Studio에서 text 또는 varchar (MAX) 열의 전체 내용을 보려면 어떻게합니까? (0) | 2020.09.24 |
std :: type_info :: name 결과 관리 해제 (0) | 2020.09.24 |