0

I am trying to send SOAP request to my web service and from this question - Client to send SOAP request and received response

I got this piece of code:

using System.Xml;
using System.Net;
using System.IO;

public static void CallWebService()
{
var _url = "http://xxxxxxxxx/Service1.asmx";
var _action = "http://xxxxxxxx/Service1.asmx?op=HelloWorld";

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

// begin async call to web request.
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

// suspend this thread until call is complete. You might want to
// do something usefull here like update your UI.
asyncResult.AsyncWaitHandle.WaitOne();

// get the response from the completed web request.
string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
    {
        soapResult = rd.ReadToEnd();
    }
    Console.Write(soapResult);        
}
}

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()
{
    XmlDocument soapEnvelopeDocument = new XmlDocument();
soapEnvelopeDocument.LoadXml(@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/1999/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/1999/XMLSchema""><SOAP-ENV:Body><HelloWorld xmlns=""http://tempuri.org/"" SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""><int1 xsi:type=""xsd:integer"">12</int1><int2 xsi:type=""xsd:integer"">32</int2></HelloWorld></SOAP-ENV:Body></SOAP-ENV:Envelope>");
return soapEnvelopeDocument;
}

private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
    soapEnvelopeXml.Save(stream);
}
} 

But it doesn't work for me, it says there is no definition for GetRequestStream() and Headers where you add the SOAPAction under HttpWebRequest, can anyone help me solve the problem?

Edit: The Errors occur on the line

using (Stream stream = webRequest.GetRequestStream())

And it gives the error

Error CS1061 'HttpWebRequest' does not contain a definition for 'GetRequestStream' and no extension method 'GetRequestStream' accepting a first argument of type 'HttpWebRequest' could be found (are you missing a using directive or an assembly reference?)

And another error on the line

 webRequest.Headers.Add("SOAPAction", action);

And it gives the error

Error CS1929 'WebHeaderCollection' does not contain a definition for 'Add' and the best extension method overload 'SettersExtensions.Add(IList, BindableProperty, object)' requires a receiver of type 'IList'

Ciaran
  • 199
  • 1
  • 2
  • 14
  • Can you please put the exact error details and the line where it occurred? – Souvik Ghosh Nov 09 '17 at 10:21
  • @SouvikGhosh I have edited the question to show where the errors occur and the error details – Ciaran Nov 09 '17 at 10:26
  • I think these methods are not supported in Xamarin. There are few workarounds you can check them at- https://stackoverflow.com/questions/14344029/system-net-httpwebrequest-does-not-contain-a-definition-for-getrequeststream, https://stackoverflow.com/questions/24321966/system-net-httpwebrequest-does-not-contain-a-definition-for-getresponse – Souvik Ghosh Nov 09 '17 at 11:20

1 Answers1

0

Use for adding headers:

webRequest.Headers["HeaderToken"] = "HeaderValue";

And for GetRequestStream use:

using (var stream = await Task.Factory.FromAsync<Stream>(webRequest.BeginGetRequestStream, webRequest.EndGetRequestStream, null))
                    {
                        reqBody.Save(stream);
                    }
Ciaran
  • 199
  • 1
  • 2
  • 14