0

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

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();

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

6
  • А вы сами проверяли? Какой ответ приходит? 24 июл 2018 в 5:42
  • @Kirill'luridSNK'F. Да, я пробовал. Соединение происходит, но в ответ получаю:<?xml version="1.0" encoding="windows-1251" standalone="yes"?> <product> <preply> <err> <ctErr> <Code>002</Code> <Text>General System Error: Java.lang.NullPointerException</Text> – 24 июл 2018 в 7:03
  • <Code>002</Code> <Text>General System Error: Java.lang.NullPointerException</Text> - вам ничего не говорит? Это я к тому, что проблема вряд ли с Вашим запросом 24 июл 2018 в 7:29
  • @Kirill'luridSNK'F. Ну я подразумеваю что либо у меня в XML есть пустые поля, либо туда ничего не приходит. 24 июл 2018 в 7:32
  • Учитывая, что вы запускаете данный проект в консольном приложении (судя по написанному коду), вам ничего не мешает проверить свой запрос перед отправкой, простым console.writeline текста вашего запроса. 24 июл 2018 в 8:08

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.