Search code examples
c#web-servicessoapportable-class-library

Trying to post SOAP request from portable class library


I am currently trying to write a portable class library in c# that will allow me to send SOAP POST requests to a web service.

Because you are limited in what you can reference when making a PCL, I cannot simply "Add a service reference." Instead, I am trying to manually make the SOAP request and I am getting stuck on adding headers to the HttpWebRequest.

My code is as follows:

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("website_here");
    byte[] xmlStr = getPullCustInfoXml(postParameters);
    request.Method = "POST";
    request.Headers.Add() <- trying to add headers here.

I can't seem to add any headers because I get the error:

    'System.Net.Http.WebHeaderCollectionExtensions.Add(System.Net.WebHeaderCollection, string, string)' is inaccessible due to its protection level

Why would it be inaccessible even after I casted the request as an HttpWebRequest?

Is there a better way to implement SOAP requests when making a portable class library?


Solution

  • I don't get the same error message when I copy and paste your code.

    Make sure HttpWebRequest is from System.Net namespace though. The documentation says Headers.Add methods are all public.

    Also, try different overload methods. There are:

    • Add(NameValueCollection)
    • Add(String)
    • Add(HttpRequestHeader, String)
    • Add(HttpResponseHeader, String)
    • Add(String, String)

    More info: http://msdn.microsoft.com/en-us/library/system.net.webheadercollection(v=vs.110).aspx