Example #1
0
        public static async Task <HttpClientResponse> GetResponse(Uri address, string requestBody, SoapClientMessage message)
        {
            //Create the HttpClient for this server
            using (HttpClient client = GetHttpClientForAddress(address.Scheme + "://" + address.Host)) {
                //Headers
                bool isSoap12 = (bool)ReflectionHelper.GetPropertyValue(message, "IsSoap12");
                if (!isSoap12)
                {
                    client.DefaultRequestHeaders.Add("SOAPAction", "\"" + message.Action + "\"");
                }

                //if we use Add instead of TryAddWithoutValidation the client will automatically add commas wherever it finds a space.
                //very ugly and probably not what we want.
                client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", UserAgent);

                //Should be:
                //User-Agent: Mono Custom HttpSoapClient 1.0
                //SOAPAction: "http://tempuri.org/Service/Method"
                //Content-Type: text/xml; charset=utf-8

                //content, with type and encoding, as string
                var content = new StringContent(requestBody, Encoding.UTF8, message.ContentType);

                //post
                using (var result = await client.PostAsync(address, content)) {
                    //response wrapper, to avoid passing params by ref
                    HttpClientResponse response = new HttpClientResponse();
                    response.DataStream = await result.Content.ReadAsStreamAsync();

                    response.StatusCode = result.StatusCode;

                    //content type, from result content
                    IEnumerable <string> values;
                    if (result.Content.Headers.TryGetValues("Content-Type", out values))
                    {
                        response.ContentType = values.FirstOrDefault();
                    }

                    return(response);
                }
            }
        }