How does Dart leverage SOAP requests?

一曲冷凌霜 提交于 2020-01-03 03:29:08

问题


I was looking up how to do SOAP requests within Dart. When looking at HTTPRequest it really only mentions RESTful services and wanted to make sure that this can be done.

Right now, I have my server, username, and password. Trying to get a successful authentication via the service, so that way I have an auth token i can pass when doing subsequent calls.

It seems for example in .NET, it does the following and then stores the credential in a server side session variable which I was using as a starting point to make this in Dart.

// create web service api object
WebServiceAPI api = new WebServiceAPI();

if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["ProxyUserName"]))
{
    System.Net.NetworkCredential nc = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["ProxyUserName"], ConfigurationManager.AppSettings["ProxyPassword"], ConfigurationManager.AppSettings["ProxyDomain"]);
    System.Net.CredentialCache cc = new System.Net.CredentialCache();
    cc.Add(new Uri(api.Url), "NTLM", nc);
    api.Credentials = cc;
}

api.AuthenticateCredential("api@admin", "admin", 0, 0);
HttpContext.Current.Session["api"] = api;

Edit: I am adding some sample data such that if there is a hack we can leverage to get something working, we might be able to abstract it and genericize.

service asmx file:  
http://127.0.0.1:1337/service.asmx

Method we will be calling: (AuthenticateCredential)
http://127.0.0.1:1337/service.asmx?op=AuthenticateCredential

The sample SOAP request:
POST /service.asmx HTTP/1.1
Host: 127.0.0.1
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://foo.com/bar/320/Default/AuthenticateCredential"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <AuthenticateCredential xmlns="http://foo.com/barr/320/Default">
      <UserName>string</UserName>
      <Password>string</Password>
      <CurrentSystemLoginID>int</CurrentSystemLoginID>
      <CurrentCustomerID>int</CurrentCustomerID>
    </AuthenticateCredential>
  </soap:Body>
</soap:Envelope>

then naturally, I will have to write up and mod the string,string,int,int out of the envelope. such that the credentials are correct.


回答1:


I've performed SOAP actions for Blackboard's web services using Dart, so it's possible. To do so, I had to build the SOAP envelope programmatically for each request. The requests themselves were sent using the http package's 'post' method.

Can't say how your requests should be set up, that would depend on the web service you're attempting to access. For the HTTP headers, I sent a 'Content-Type' of 'text/xml; charset=utf-8' and a 'SOAPAction' header specifying the SOAP method. The body was the full SOAP envelope.

You may need to play around a bit to build the envelope with the correct format/info your service expects. I used the xml package to parse/interpret the responses.




回答2:


AFAIK there is no solution for that. There are only limited XML packages for Dart and I haven't seen attempts to implement SOAP itself. A possible workaround would be to delegate to a server written in another language that forwards REST calls as SOAP calls.




回答3:


You can send SOAP request using HTTP POST in DART. Here's how you do it.

Sample web service ( GlobalWeather )

var envelope = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><GetCitiesByCountry  xmlns=\"http://www.webserviceX.NET\"><CountryName>Pakistan</CountryName>    </GetCitiesByCountry></soap:Body></soap:Envelope>";

http.Response response = await http.post(
        'http://www.webservicex.net/globalweather.asmx',
        headers: {
          "Content-Type": "text/xml; charset=utf-8",
          "SOAPAction": "http://www.webserviceX.NET/GetCitiesByCountry",
          "Host": "www.webservicex.net"
          //"Accept": "text/xml"
        },
        body: envelope
    );

 print.(response.body);


来源:https://stackoverflow.com/questions/35139913/how-does-dart-leverage-soap-requests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!