Отправка XML POST запросом на C#

204
23 сентября 2018, 22:10

Есть у меня задачка. Необходимо отправлять и принимать ответы от веб-сервиса. Вот данные из инструкции.

Request and response information is transmitted by placing an XML document in the POST payload of the HTTPS request. To integrate with the iCRS system, do the following: 1. Build/Use a client program that can operate with the HTTPS protocol. There are no restrictions on the type of client program used, providing it can use this protocol and deal with transactions synchronously. 2. Build/Use a program that can build a valid request XML document (see specification in Appendix A) and attach it via a POST parameter to a HTTPS request. POST / HTTP/1.0 UserAgent: HTTPTool/1.0 ContentType: text/xml Post Payload 3. Build/Use a program that can retrieve the HTTPS response and process the binary file output, which includes the XML payload and attached digital signature. See the Extracting the XML Output section on page 9. 4. Build/Use a program that can correctly interpret the inquiry response information within the XML return.

Т.е. на сколько я понял, необходимо создать XML и обычным POST запросом отправить, и получить ответ?

XML делаю обычным XElement

XElement xml = new XElement("Product",
  new XElement("prequest",
    new XElement("req",
      new XElement("AddressReq",
        new XElement("street", "Горького"),
        new XElement("houseNumber", "1"),
        new XElement("apartment", "38"),
        new XElement("city", "Магадан"),
        new XElement("postal", "685000"),
        new XElement("addressType", "1")))));

Далее делаю запрос и ответ:

Console.WriteLine(xml);
Console.ReadKey();
WebRequest request = WebRequest.Create("http://websevice.com/url");
byte[] byteArray = Encoding.GetEncoding(1251).GetBytes(xml.ToString());
request.Method = "POST";
request.ContentType = "text/xml";
request.ContentLength = byteArray.Length;
Stream data = request.GetRequestStream();
data.Write(byteArray, 0, byteArray.Length);
data.Close();
Console.ReadKey();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
data = response.GetResponseStream();
StreamReader reader = new StreamReader(data);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
reader.Close();
data.Close();
response.Close();
Console.ReadKey();

Вопрос. Правильно ли я все делаю?

READ ALSO
VuforiaConfiguration - Webcam - Camera Device

VuforiaConfiguration - Webcam - Camera Device

Доброго времени суток

203
Как работать с ChildRelations в дата сете?

Как работать с ChildRelations в дата сете?

Есть дата сет с кучей ChildRelations связейСвязи называются ТаблицаГлавная_ПотомокГлавной, и так внутрь до 6 вложений

191
Async await в 3-х методах C#

Async await в 3-х методах C#

Мне нужно запустить в консольном приложение 3 асинхронных 3 методаКаждый метод создает бесконечный цикл в котором обрабатывает данные из источника...

198
C# WPF MVVM window dragmove

C# WPF MVVM window dragmove

Есть кастомное окно, как сделать в MVVM dragmove для него? Как и где правильно забиндить mouse event?

249