Git Product home page Git Product logo

soaphttpclient's Introduction

SoapHttpClient NuGet

A lightweight wrapper of an HttpClient (using IHttpClientFactory) for POSTing messages that allows the user to send the SOAP Body and Header (if needed) without caring about the envelope.

Changelog

3.0.0

  • Replaced HttpClient with HttpClientFactory.

2.2.1

  • Added support for Cancellation Tokens.

2.2.0

  • Updated codebase.
  • Migrated test project to .net core app.
  • Fixed an error of SOAPAction not being sent.

2.1.0

  • Updated to NetStandardLibrary 2.0
  • Fixed a bug where an extension method was calling himself recursively

2.0.0

  • Major refactor to the codebase.
  • Added the functionality of adding more than one header and/or body in the envelope.
  • The ctor will no longer determine the SoapVersion, since it is a message property and the API should be ignorant about this.
  • [BREAKING CHANGE]: SoapVersion is now required for every message.
  • [BREAKING CHANGE]: Removed methods where the endpoint was a string instead of an uri.

API

Constructors

SoapClient()

Initializes SoapClient with a default IHttpClientFactory that implements automatic decompression.

SoapClient(IHttpClientFactory httpClientFactory)

Initializes SoapClient with a IHttpClientFactory provided by the caller. The IHttpClientFactory is the new interface used to manage HttpClient introduced on .NET Core 2.1.


Methods

All Methods and Extension Methods returns a Task of HttpResponseMessage

The interface makes the client implement the following method:

Task<HttpResponseMessage> PostAsync(
	Uri endpoint,
	SoapVersion soapVersion,
	IEnumerable<XElement> bodies,
	IEnumerable<XElement> headers = null,
	string? action = null,
	CancellationToken cancellationToken = CancellationToken.Default);

Allowing us to send the following calls:

  • Uri / Version / Bodies
  • Uri / Version / Bodies / Headers
  • Uri / Version / Bodies / Headers / Action

Then there are sugar sintax extension methods:

Task<HttpResponseMessage> PostAsync(
	this ISoapClient client,
	Uri endpoint,
	SoapVersion soapVersion,
	XElement body,
	XElement header = null,
	string? action = null,
	CancellationToken cancellationToken = CancellationToken.Default);

Task<HttpResponseMessage> PostAsync(
	this ISoapClient client,
	Uri endpoint,
	SoapVersion soapVersion,
	IEnumerable<XElement> bodies,
	XElement header,
	string? action = null,
	CancellationToken cancellationToken = CancellationToken.Default);

Task<HttpResponseMessage> PostAsync(
	this ISoapClient client,
	Uri endpoint,
	SoapVersion soapVersion,
	XElement body,
	IEnumerable<XElement> headers,
	string? action = null,
	CancellationToken cancellationToken = CancellationToken.Default);

Allowing us to send the following calls:

  • Uri / Version / Body
  • Uri / Version / Body / Header
  • Uri / Version / Body / Header / Action
  • Uri / Version / Bodies / Header
  • Uri / Version / Bodies / Header / Action
  • Uri / Version / Body / Headers
  • Uri / Version / Body / Headers / Action

With all of these variants we can send a message with:

  • 1 Body - 1 Header
  • 1 Body - N Headers
  • N Bodies - 1 Header
  • N Bodies - N Headers

There are also extension methods for sync calls: However we always recommend using async programming when you are able.

Their method name is Post and their return type is HttpResponseMessage

Finally, we have extensions methods for using bodies and headers as objects and using serialization the default or a custom IXElementSerializer to serialize those objects to XElement.

Usage Examples

Controlling the Media Type

As the SoapHttpClient wraps a HttpClient you can control all aspects of the HTTP Request using a HttpMessageHandler:

public class ContentTypeChangingHandler : DelegatingHandler
{
  public ContentTypeChangingHandler(HttpMessageHandler innerHandler) : base(innerHandler) { }

  protected async override Task<HttpResponseMessage> SendAsync(
    HttpRequestMessage request,
    CancellationToken cancellationToken)
  {
    request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml; charset=utf-8");
    return await base.SendAsync(request, cancellationToken);
  }
}

Call NASA

async Task CallNasaAsync()
{
    var soapClient = new SoapClient();
    var ns = XNamespace.Get("http://helio.spdf.gsfc.nasa.gov/");

    var result =
        await soapClient.PostAsync(
            new Uri("http://sscweb.gsfc.nasa.gov:80/WS/helio/1/HeliocentricTrajectoriesService"),
            SoapVersion.Soap11,
            body: new XElement(ns.GetName("getAllObjects")));

    result.StatusCode.Should().Be(HttpStatusCode.OK);
}

Result of Calling NASA Heliocentric Trajectories Service

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:getAllObjectsResponse xmlns:ns2="http://helio.spdf.gsfc.nasa.gov/">
         <return>
            <endDate>1993-01-01T00:00:00Z</endDate>
            <id>0001</id>
            <name>COMET GRIGG-SKJLP</name>
            <startDate>1992-01-01T00:00:00Z</startDate>
         </return>
         <return>
            <endDate>1996-03-02T00:00:00Z</endDate>
            <id>0002</id>
            <name>COMET H-M-P</name>
            <startDate>1996-01-01T00:00:00Z</startDate>
         </return>
         <return>
            <endDate>1997-12-31T00:00:00Z</endDate>
            <id>0003</id>
            <name>COMET HALE-BOPP</name>
            <startDate>1997-01-01T00:00:00Z</startDate>
         </return>
         .
         .
         .
      </ns2:getAllObjectsResponse>
   </S:Body>
</S:Envelope>

Soap Icon Created by Jakob Vogel from the Noun Project

soaphttpclient's People

Contributors

bkanteruk avatar dashkan avatar pmorelli-origin avatar pmorelli92 avatar richardslater avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

soaphttpclient's Issues

Header should be SOAPAction for Soap 1.1 instead of ActionHeader

I'm using the SoapClient to communicate with a SOAP 1.1 service. I got an error that the action wasn't defined even though I specified an action when I called to post async. I discovered this is because the HTTP Request Header that the SoapClient sets is "ActionHeader" but I believe the SOAP 1.1 specification requires a "SOAPAction" header (https://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383528).

Any chance you can change line 80 of SoapClient.cs to use "SOAPAction" instead of "ActionHeader"?

No authorization options?

Hey, im trying to use your package but there is no way to use any authorization option i am right? I have a SOAP Service that requires Basic Authorization for example. Thanks!

How to Add Authorization with Bearer Token to Soap Request -> Add Soap Action

I want to use your SoapHttpClient for making Soap Request but I have difficulties in setting it up. Biggest hurdle is adding a Bearer Token to the SOAP Request.

In the SOAP UI I can add a Bearer token like this:
image

How can I do the same thing trough code? (@RichardSlater @pmorelli92 @pmorelli-origin)


What I tried in steps:

  1. Generating the Token trough the TokenClient of IdentityModel package.
  2. Add it as a header like this:
var header = new XElement("Authorization", $"Bearer {token}");
  1. Sending the request.
using (var soapClient = new SoapClient())
{
    var result =
        await soapClient.PostAsync(
            endpoint: endpoint,
            soapVersion: SoapVersion.Soap11,
            body: body,
            header: header);

            Log.Information(await result.Content.ReadAsStringAsync());
}
  1. Inspecting the result
    Internal Server Error
    I think this is most probably because I didn't add the token in the right way.

get SOAP envelope XML

Hi.

How can I catch/get the entire SOAP envelope XML that is sent? Mi goal is to get the entire message that have been sent and save it.

Actually I am saving the XML body (the one that I pass to PostAsync function) but I need the entire message on SOAP.

Edit timeout

Hi.
A process gives me an error 'The operation was canceled.'
My question is whether the timeout can be increased and how much the default would be.

sorry my english T.T

Support CancellationTokens

@pmorelli92 ,

Thanks for the great library. I added support for cancellation tokens in a PR #10 . Can you take a look to see if it's something you are willing to add to the project?

Thanks!

How to specify method name?

Hi,

I want to call web service which expect following:

POST ... HTTP/1.1
Host: ...
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "..."

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope ...>
  <soap:Body>
    <METHOD_NAME xmlns="...">
      <requestData />
    </METHOD_NAME >
  </soap:Body>
</soap:Envelope>

How to tell SoapHttpClient about METHOD_NAME?

Need to set different access user login token for the test suites.

I have created SOAPUI project with OAuth 2.0 access token. I need to set the different user access token in different Test Suites, But now if I add access token for one test step it gets applied to all, here I need different user access for different testsuites.

Not Compatible with .Net Standard 2.0

:(

I was hoping to use this in a very large project, but I need to use it in a .Net Standard 2.0 library.

Since it uses HttpClientFactory, it's not compatible with .Net Standard 2.0.

Is there an older working version that is Standard compatible? Or perhaps there is a workaround, such as an alternative constructor that takes an HttpClient instead of factory?

Thanks,
James

how to setup authorization?

Foundational http request message below:

Http 1.1

...headers...

Authorization: Basic [Base64 encoded with user name : password...]

In this situation, how to setup "Authorization" header by SoapHttpClient?

thank you very much

How to Parse the XML Response?

I have tried the NASA SOAP API though it is working properly. I got the XML String.

How to parse it to object to loop and show the result?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.