Xamarin Forms Отказаться от соединения вызов WS - PullRequest
1 голос
/ 06 января 2020

У меня есть этот класс, который работает правильно из консольного приложения. Когда я включаю его в свой проект Xamarin Forms, он выдает ошибку (отказано в соединении) при создании веб-запроса. Я был бы признателен за любую помощь в этом отношении.

using (Stream stream = webRequest.GetRequestStream()) { soapEnvelopeXml.Save(stream); } остается мертвым, пока не возникнет исключение в формах Xamarin.

Изображение ошибки

Мой код:

public class WebServiceSOAP
    {
    var _url = "http://MyDomain/My.asmx";
    var _action = "http://MyDomain/MyMethod";



string sxml = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" 
                 xmlns:adh=""http://Mydomain"">
                 <soapenv:Header/>
                 <soapenv:Body>
                 <adh:MyMethod>
                 <adh:param1>TEST1</adh:param1>
                 <adh:param2>test2</adh:param2>
                 <adh:param3>test3</adh:param3>
                 </adh:MyMethod> 
                 </soapenv:Body>
                 </soapenv:Envelope>";

XmlDocument soapEnvelopeXml = CreateSoapEnvelope(sxml);
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

using (var response = (HttpWebResponse)(webRequest.GetResponse()))
{
     HttpStatusCode code = response.StatusCode;
     if (code == HttpStatusCode.OK) {
          using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
          {
               string resp = sr.ReadToEnd();
          }
      }
 }
 }

  private static HttpWebRequest CreateWebRequest(string url, string action)
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Headers.Add("SOAPAction", action);
        webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        webRequest.Accept = "text/xml";
        webRequest.Method = "POST";
        return webRequest;
    }

    private static XmlDocument CreateSoapEnvelope(string sxml)
    {
        XmlDocument soapEnvelopeDocument = new XmlDocument();
        soapEnvelopeDocument.LoadXml(sxml);
        return soapEnvelopeDocument;
    }

    private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
    {
        using (Stream stream = webRequest.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...