Sledzenie przesyłek z Poczty Polskiej

0

Witam

mam problem, probuje stworzyc aplikacje wykorzystujaca webservice Poczty Polskiej do sledzenia przesylek. Dokumentacja jest troche lakoniczna w tym wzgledzie (Poczta stwierdzila, ze nie ma zadnych przykladow, ktore mi moze podeslac), wiec zabralem sie za zrobienie tego sam (niestety mam srednie doswiadczenie w tym wzgledzie).

No i natrafilem na problem, poniewaz przy probie odpalenia takiego kodu:

sledzenie.Sledzenie wSledz = new sledzenie.Sledzenie();

System.Net.NetworkCredential c = new System.Net.NetworkCredential();
c.UserName = "sledzeniepp";
c.Password = "PPSA";
System.Net.CredentialCache cc = new System.Net.CredentialCache();
cc.Add(new Uri("https://tt.poczta-polska.pl/Sledzenie/services/Sledzenie?wsdl"), "Basic", c);
wSledz.Credentials = cc;

string ff = wSledz.witaj("ssss");

Dostaje blad:

WSDoAllReceiver: Incoming message does not contain required Security header

Czy ktos z Was probowal sie kiedys "podpiac" pod Webservice sledzenia przesylek i mu sie udalo?

Z gory dzieki za pomoc
Pozdrawiam
Krzysiek

0

Odpowiedź na Twoje pytanie jest mało istotna, ważniejszy jest ten błąd... Incoming message does not contain required Security header oznacza że wysłana wiadomość nie ma jakiegoś headera bezpieczeństwa. Wygoogluj jak się robi poprawnie takie zapytania.
Może to Ci pomoże: http://www.codeproject.com/Articles/27365/Authenticate-NET-Web-Service-with-Custom-SOAP-Head

0

Ale z tym zabawy ale się udało.

  1. Add Service Reference -> Add Web Reference -> https://tt.poczta-polska.pl/Sledzenie/services/Sledzenie?wsdl -> Add Reference
  2. NuGet -> Microsoft.Web.Services3
  3. Otwieramy to co nam VS wygenerował jako WS i podmieniamy:
public partial class Sledzenie : System.Web.Services.Protocols.SoapHttpClientProtocol

na

public partial class Sledzenie : Microsoft.Web.Services3.WebServicesClientProtocol
  1. Tworzymy kolejną klasę w celu wywalenia TimeStamp z zapytania i pozostawienia reszt autentykacji. Jak ktoś potrafi to zrobić ładniej żeby nie wywalało całości to byłbym wdzięczy za info.
   public class ClientOutputFilter : SoapFilter
   {
      public override SoapFilterResult ProcessMessage(SoapEnvelope envelope)
      {
         XmlNode securityNode = envelope.CreateNode(XmlNodeType.Element, "wsse:Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
         XmlAttribute securityAttr = envelope.CreateAttribute("soap:mustUnderstand");
         securityAttr.Value = "1";
         XmlNode usernameTokenNode = envelope.CreateNode(XmlNodeType.Element, "wsse:UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
         XmlElement userElement = usernameTokenNode as XmlElement;
         userElement.SetAttribute("xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
         XmlNode userNameNode = envelope.CreateNode(XmlNodeType.Element, "wsse:Username", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
         userNameNode.InnerXml = "sledzeniepp";
         XmlNode pwdNode = envelope.CreateNode(XmlNodeType.Element, "wsse:Password", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
         XmlElement pwdElement = pwdNode as XmlElement;
         pwdElement.SetAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
         pwdNode.InnerXml = "PPSA";
         usernameTokenNode.AppendChild(userNameNode);
         usernameTokenNode.AppendChild(pwdNode);
         securityNode.AppendChild(usernameTokenNode);
         envelope.ImportNode(securityNode, true);
         XmlNode node = envelope.Header;
         node.AppendChild(securityNode);
         return SoapFilterResult.Continue;
      }
   }
    public class MyAssertion : PolicyAssertion
    {
        public override SoapFilter CreateClientInputFilter(FilterCreationContext context)
        {
            return null;
        }

        public override SoapFilter CreateClientOutputFilter(FilterCreationContext context)
        {
           return new ClientOutputFilter();
        }

        public override SoapFilter CreateServiceInputFilter(FilterCreationContext context)
        {
            return null;
        }

        public override SoapFilter CreateServiceOutputFilter(FilterCreationContext context)
        {
            return null;
        }
    }
  1. Wracamy do naszego wygenerowanego WS i w konstruktorze dodajemy taki oto wpis:
Policy policy = new Policy();
policy.Assertions.Add(new MyAssertion());
this.SetPolicy(policy);
  1. Odpalamy:
private static void Main(string[] args)
{
         var sledzenie = new Sledzenie();
         Console.WriteLine(sledzenie.witaj("4Programmers.net"));
}

Dołączyłem projekt na którym kombinowałem jak to odpalić.

1

Wersja numer dwa:

  1. Add Service Reference -> https://tt.poczta-polska.pl/Sledzenie/services/Sledzenie?wsdl -> Ok
  2. Custom Binding
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/></startup><system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="SledzenieSoap11Binding">
                    <security mode="Transport" />
                </binding>
                <binding name="SledzenieSoap11Binding1" />
            </basicHttpBinding>
           <customBinding>
              <binding name="CustomBindingName">
                 <security authenticationMode="UserNameOverTransport" includeTimestamp="false">
                    <secureConversationBootstrap />
                 </security>
                 <textMessageEncoding messageVersion="Soap11" />
                 <httpsTransport useDefaultWebProxy="false" requireClientCertificate="false" />
              </binding>
           </customBinding>
        </bindings>
        <client>
            <endpoint address="https://tt.poczta-polska.pl/Sledzenie/services/Sledzenie.SledzenieHttpSoap11Endpoint/"
                binding="customBinding" bindingConfiguration="CustomBindingName" 
                contract="WcfPoczta.SledzeniePortType" name="SledzenieHttpSoap11Endpoint" />
        </client>
    </system.serviceModel>
</configuration>

(Informacja) Olałem tutaj wszelkie certyfikaty.
3. Kod C#:

      static void Main(string[] args)
      {
         SledzeniePortTypeClient service = new SledzeniePortTypeClient();
         service.ClientCredentials.UserName.UserName = "sledzeniepp";
         service.ClientCredentials.UserName.Password = "PPSA";
         Console.WriteLine(service.witaj("4Programmers.net"));
      }
  1. Projekt:
0

hej
dziala!! - wielkie dzieki!!!!!!!
Pozdrawiam
Krzysiek

0

Sorki, że odświeżam, ale nagimnastykowałem się z tym niemało i założyłem tu konto bo chciałem podziękować przedmówcy za pomoc po latach. Śmiesznie, że 5 lat temu dodano soap'a i nadal się go używa :D Jest tyle lepszych rozwiązań...

0

Mamy 2023 rok, nadal nic się nie zmieniło...

Moje rozwiązanie dla dot net core C#.

Metoda Witaj

public void GetStatusPrzesylki() 
{
    SledzeniePortTypeClient tEN = new SledzeniePortTypeClient(
        GetBindingTracking(),
        new EndpointAddress("https://tt.poczta-polska.pl/Sledzenie/services/Sledzenie.SledzenieHttpSoap11Endpoint/")
        );
    tEN.ClientCredentials.UserName.UserName = "sledzeniepp";
    tEN.ClientCredentials.UserName.Password = "PPSA";
    Console.WriteLine(tEN.witaj("4Programmers.net"));
}

Custom Binding

public static Binding GetBindingTracking()
{
    CustomBinding customBinding = new CustomBinding();
    var securityBindingElement2 = TransportSecurityBindingElement.CreateUserNameOverTransportBindingElement();
    securityBindingElement2.IncludeTimestamp = false;
    customBinding.Elements.Add(securityBindingElement2);
    var messageelement = new TextMessageEncodingBindingElement(MessageVersion.CreateVersion(EnvelopeVersion.Soap11, AddressingVersion.None), Encoding.UTF8);
    customBinding.Elements.Add(messageelement);
    var transportElement = new HttpsTransportBindingElement();
    transportElement.MaxReceivedMessageSize = 20000000; // 20 megs
    transportElement.UseDefaultWebProxy = false;
    transportElement.RequireClientCertificate = false;
    customBinding.Elements.Add(transportElement);

    return customBinding;
}

w .project zmieniamy, (zmiany wymagane dla dot net core 2.2, 3.0, wyżej powinno być ok.

<PackageReference Include="System.ServiceModel.Duplex" Version="4.4.*" />
<PackageReference Include="System.ServiceModel.Http" Version="4.4.*" />
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.4.*" />
<PackageReference Include="System.ServiceModel.Security" Version="4.4.*" />

Zmieniamy na: (testowane dla dot net core 2.2 i 3.0)

<PackageReference Include="System.ServiceModel.Duplex" Version="4.7.0" />
<PackageReference Include="System.ServiceModel.Http" Version="4.7.0" />
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.7.0" />
<PackageReference Include="System.ServiceModel.Security" Version="4.7.0" />

1 użytkowników online, w tym zalogowanych: 0, gości: 1