처음으로 XML 데이터 집합을 URL에 게시하여 공급 업체의 API를 사용해야합니다. http://meyerweb.com/eric/tools/dencoder/을 사용하여 데이터 세트를 인코딩하고 브라우저에 URL & 데이터 세트를 수동으로 붙여 넣으면 성공적인 응답을받습니다.URL에 게시 할 XML 문자열을 .NET에서 올바르게 인코딩하는 방법은 무엇입니까?
그러나 .NET에서 동일한 작업을 수행 할 때 공급 업체 API에서 내 데이터 집합이 유효하지 않음을 나타내는 응답을받습니다.
public void PostXML(string value1, string value2, string value3, out string responseStatus, out string responseBody)
{
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create("https://www.vendorURL.com/API/page.html?input=");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "<DATASET>";
postData += "<FIELD1>" + value1+ "</FIELD1>";
postData += "<FIELD2>" + value2 + "</FIELD2>";
postData += "<FIELD3>" + value3 + "</FIELD3>";
postData += "</DATASET>";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Assign the status.
responseStatus = ((HttpWebResponse)response).StatusDescription;
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
responseBody = reader.ReadToEnd();
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
}
내가
오전는 공급 업체 API로부터 응답을 받고, 유의 사항 :
여기에 (내가 좋은 팔자 'interweb에 다른 곳에서 복사) 내 코드입니다. 내 데이터 세트가 유효하지 않다는 말입니다. 이미 해당 공급 업체의 고객 지원에 문의했습니다. 그게 내 데이터 세트 자체가 올바른지 확인했지만, 어딘가에 내가 잘못 공급 업체 API에 의해 인식 할 수 있도록 그것을 인코딩 해요.
@user : "C# .NET"과 같은 것이 없습니다. 언어는 "C#"입니다. 프레임 워크는 ".NET"입니다. –
"수동으로 URL 및 데이터 세트를 브라우저에 붙여 넣기"라고 말하면 URL과 데이터 세트를 검색 주소창에 함께 붙여 넣을 것입니까? 그렇다면 HTTP POST 대신 HTTP GET을 말합니다. – kbrimington
@kbrimington : 예, 그것이 제가 의미했던 것입니다. – Atrabius