OWASP O2 Platform Blog

O2 Script: consume webservices by using the WSDL’s C#

(as a follow from O2 Script: Using Reflection to Invoke Bing Search Web Service) Here is how one can consume webservices by using a (previously created) C# of the WSDL:

var _GlobalWeather = new GlobalWeather();
_GlobalWeather.GetWeather("Porto", "Portugal");

return _GlobalWeather;

//O2File:C:\O2\_tempDir\4-3-2011\wsdl_tmpC7EC\GlobalWeather.cs
//O2Ref:System.Web.Services.dll

//Note: the GlobalWeather.cs file was created with the command:
//
//     return new DotNet_SDK_WSDL().wsdl_CreateCSharp("http://www.webservicex.net/globalweather.asmx?WSDL");
//     //O2File:DotNet_SDK_WSDL.cs
//     //using O2.XRules.Database.Languages_and_Frameworks.DotNet

Here is Bing API version, note how (when compared with this version (O2 Script: Using Reflection to Invoke Bing Search Web Service) which is reflection based) all variable references are strongly typed)

var AppID = @"C:\O2\_USERDATA\accounts.xml".credential("Bing_API").password();</pre>
var _BingService = new BingService();

var _parameters = new SearchRequest();

_parameters.AppId = AppID;
_parameters.Sources = new SourceType[] { SourceType.Web };
_parameters.Query = "OWASP";

var response = _BingService.Search(_parameters );
return response.Web.Results;

//O2File:C:\O2\_tempDir\4-3-2011\wsdl_tmp107F\BingService.cs
//O2Ref:System.Web.Services.dll
//O2File:SecretData_ExtensionMethods.cs

April 3, 2011 Posted by | .NET, WS | 1 Comment

O2 Script: Using Reflection to Invoke Bing Search Web Service

These script makes extensive use of O2’s reflection APIs to dynamically consume and invoke an WSDL.

 Here is a Weather WebService:

var wsdlToLoad = @"http://www.webservicex.net/globalweather.asmx?WSDL";
var wsdlAssembly = new DotNet_SDK_WSDL().wsdl_CreateAssembly(wsdlToLoad);
var wsdlMethods = wsdlAssembly.webService_SoapMethods();
var getCitiesByCountry = assembly.type("GlobalWeather").method("GetCitiesByCountry") ;
//.method("GetCitiesByCountry"); var globalWeather = assembly.type("GlobalWeather").ctor();

return globalWeather.invoke("GetCitiesByCountry", "Portugal");

//return globalWeather.invoke("GetWeather", "Lisboa", "Portugal");

//O2File:DotNet_SDK_WSDL.cs
//using O2.XRules.Database.Languages_and_Frameworks.DotNet

Here is Bing’s search API:

var wsdlToLoad = "http://api.search.live.net/search.wsdl";

var assembly = new DotNet_SDK_WSDL().wsdl_CreateAssembly(wsdlToLoad); //cache the created assembly to make it faster
//var assembly = @"C:\Users\o2\AppData\Local\Temp\f3kx9cxv.dll".assembly();

var searchBingFor = "OWASP";
var currentType = "BingService";
var currentMethod = "Search";
var AppID = @"C:\O2\_USERDATA\accounts.xml".credential("Bing_API").password();

var searchRequest = assembly.type("SearchRequest").ctor();

searchRequest.property("Query", searchBingFor);
searchRequest.property("AppId", AppID);
var sourceType = assembly.type("SourceType");
var webEnum = sourceType.enumValue("Web");
var sources = sourceType.createArray(webEnum);

var methodParameters = assembly.type(currentType).method(currentMethod).create_LiveObject_From_MethodInfo_Parameters();
searchRequest.property("Sources", sources);

methodParameters.property("parameters",searchRequest);

var response = assembly.type(currentType).ctor()
.invoke(currentMethod, methodParameters.getProperties_AsArray());

return response.property("Web").property("Results") ;

//using System.Reflection
//using O2.XRules.Database.Languages_and_Frameworks.DotNet
//O2File:DynamicTypes.cs
//O2File:DotNet_SDK_WSDL.cs
//O2File:SecretData_ExtensionMethods.cs

April 3, 2011 Posted by | .NET, WS | 2 Comments

Using a webservice to get the IP Address

These scripts show how to use this xml feed to access information about the ‘current user IP address’.

This first code snippet shows how to get the IP address value from the downloaded xml file:

var currentIpAddress = @"<a href="http://ip-address.domaintools.com/myip.xml">http://ip-address.domaintools.com/myip.xml</a>"
       .uri()
       .getHtml()
       .xRoot()
       .element("ip_address")
       .value();
return currentIpAddress; 

 

This second example will show a more advanced script which downloads the XML feed  and displays it in table list (the “IP Address history” was added for the cases where one wants to confirm a IPAddress change (for example in the case of connecting to a VPN)):

//var topPanel = panel.clear().add_Panel();
var topPanel = O2Gui.open<Panel>("IPAddress", 650,400);
var tableList = topPanel.add_TableList("IP Details (select to copy value to clipboard)");
tableList.afterSelect(
 (selectedRows)=>{      
      if(selectedRows.notNull() &&  selectedRows.size()==1)      
       selectedRows[0].values()[1].toClipboard();       
     });

var ipAddress = tableList.insert_Above<Panel>(40)
         .add_Label("Your current IP is:").size(20)
         .append_Label("...").size(20).autoSize();
var history = tableList.insert_Right<Panel>(250).add_GroupBox("IPAddress History").add_TextArea();
Func<Dictionary<string,string>> getIpData =
 ()=>{
   var ipServiceUrl = @"<a href="http://ip-address.domaintools.com/myip.xml">http://ip-address.domaintools.com/myip.xml</a>";
   var xmlData = ipServiceUrl.uri().getHtml();   
   if (xmlData.valid())
   {  
    var ipData = new Dictionary<string,string>();
    foreach(var element in xmlData.xRoot().elements())
     ipData.Add(element.name(), element.value());    
    return ipData;    
   }
   return null;
  };

Action showIpData =
 ()=>{
   tableList.clearTable();
   ipAddress.set_Text("fetching data...").textColor(Color.DarkGreen); 
   var ipData = getIpData(); 
   if(ipData.notNull())
   {    
    tableList.show(ipData).makeColumnWidthMatchCellWidth();        
    ipAddress.set_Text(ipData["ip_address"]).textColor(Color.DarkBlue);
    history.append_Line("[{0}]: {1}".format(DateTime.Now.str(),ipData["ip_address"]));
   }
   else   
    ipAddress.set_Text("error!!!").textColor(Color.Red); 
  };

ipAddress.append_Link("refresh", ()=>showIpData()).left(450).top(12);

showIpData();
return "ok";

Note: this script is included in the Dynamic Scripts included with O2: Util – View Current IP Address info.h2

November 26, 2010 Posted by | WS | Leave a comment