Last active
July 24, 2020 16:25
-
-
Save dylanlangston/8309e20512ce369e1c3205058ed68e34 to your computer and use it in GitHub Desktop.
Simple C# program for sending and receiving APIs to PaperVision Enterprise, useful as a reference. Uses older HttpWebRequest instead of prefered HttpClient.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Net; | |
using System.Xml; | |
using System.IO; | |
using System.Text.RegularExpressions; | |
namespace dlangston | |
{ | |
class Program | |
{ | |
// Vars | |
public static string url; | |
public static string entityid; | |
public static string username; | |
public static string password; | |
public static string result; | |
public static string sessionID; | |
// Calls the PVE HTTPInterface.aspx with XML request | |
private static string CallHTTPInterface(string Url, string XML) | |
{ | |
HttpWebRequest request = CreateWebRequest(Url); | |
XmlDocument soapEnvelopeXml = new XmlDocument(); | |
soapEnvelopeXml.LoadXml(XML); | |
using (Stream stream = request.GetRequestStream()) | |
{ | |
soapEnvelopeXml.Save(stream); | |
} | |
using (WebResponse response = request.GetResponse()) | |
{ | |
using (StreamReader rd = new StreamReader(response.GetResponseStream())) | |
{ | |
string soapResult = rd.ReadToEnd(); | |
return soapResult; | |
} | |
} | |
} | |
// Create a soap webrequest to [Url] | |
private static HttpWebRequest CreateWebRequest(string URL) | |
{ | |
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(URL + "httpinterface.aspx"); | |
if (URL.ToLower().Contains("httpinterface.aspx")) | |
{ | |
webRequest = (HttpWebRequest)WebRequest.Create(URL); | |
} | |
webRequest.Headers.Add(@"SOAP:Action"); | |
webRequest.ContentType = "text/xml;charset=\"utf-8\""; | |
webRequest.Accept = "text/xml"; | |
webRequest.Method = "POST"; | |
return webRequest; | |
} | |
// Get SessionID from results | |
public static string getSessionID(string result) | |
{ | |
string sessionID = null; | |
try | |
{ | |
XmlDocument doc = new XmlDocument(); | |
doc.LoadXml(result); | |
sessionID = doc.DocumentElement.SelectSingleNode("/*/SESSIONID").InnerText; | |
} | |
catch { } | |
//Validate ID exists or throw error message | |
if (sessionID == null || sessionID == "") | |
{ | |
throw new System.ArgumentException("SessionID Error", result); | |
} | |
return sessionID; | |
} | |
// Log the User in, returns XML from site. | |
public static string LoginRequest(string URL, string ENT, string USR, string PWD) | |
{ | |
// Login Request XML | |
string loginRequest = "<LoginUserEx3_REQUEST>" + | |
"<FUNCTION>" + | |
"<NAME>LOGINUSEREX3</NAME>" + | |
"<PARAMETERS>" + | |
"<ENTITYID>" + ENT + "</ENTITYID>" + | |
"<USERNAME>" + USR + "</USERNAME>" + | |
"<PASSWORD>" + PWD + "</PASSWORD>" + | |
"<SOURCEIP></SOURCEIP>" + | |
"<CORELICTYPE></CORELICTYPE>" + | |
"<CLIENTPINGABLE>0</CLIENTPINGABLE>" + | |
"<REMOTEAUTH>0</REMOTEAUTH>" + | |
"</PARAMETERS>" + | |
"</FUNCTION>" + | |
"</LoginUserEx3_REQUEST>"; | |
// Send XML and get response | |
return CallHTTPInterface(URL, loginRequest); | |
} | |
public static string LogoutRequest(string URL, string ENT, string sess) | |
{ | |
string logoutRequest = "<KillSession_REQUEST>" + | |
"<FUNCTION>" + | |
"<NAME>KILLSESSION</NAME>" + | |
"<PARAMETERS>" + | |
"<ENTITYID>" + ENT + "</ENTITYID>" + | |
"<SESSIONID>" + sess + "</SESSIONID>" + | |
"<SOURCEIP></SOURCEIP>" + | |
"</PARAMETERS>" + | |
"</FUNCTION>" + | |
"</KillSession_REQUEST>"; | |
// Set sessionID to Null | |
sessionID = null; | |
// Send XML and get response | |
return CallHTTPInterface(URL, logoutRequest); | |
} | |
// Check for arguments and prompt for any missing ones | |
private static string checkForArgs(string argsstring, List<String> argslist, string argument, string name, string regex) | |
{ | |
string val; | |
if (argslist.Contains(argument)) | |
{ | |
var groups = Regex.Match(argsstring, $"({argument} |{argument})({regex})( /|$)").Groups; | |
val = groups[2].Value; | |
Regex r = new Regex("^" + regex + "$"); | |
if (!(r.IsMatch(val))) | |
{ | |
while (!(r.IsMatch(val))) | |
{ | |
Console.WriteLine($"Invalid {name} - '{val}'"); | |
Console.Write($"Type your {name}: "); | |
val = Console.ReadLine(); | |
} | |
} | |
} | |
else | |
{ | |
Console.Write($"Type your {name}: "); | |
val = Console.ReadLine(); | |
Regex r = new Regex("^" + regex + "$"); | |
if (!(r.IsMatch(val))) | |
{ | |
while (!(r.IsMatch(val))) | |
{ | |
Console.WriteLine($"Invalid {name}"); | |
Console.Write($"Type your {name}: "); | |
val = Console.ReadLine(); | |
} | |
} | |
} | |
return val; | |
} | |
// Called on Exit | |
private static void Exit() | |
{ | |
// Log out of active sessionID | |
if (!(sessionID == null || sessionID == "")) | |
{ | |
LogoutRequest(url, entityid, sessionID); | |
} | |
// Exit | |
System.Environment.Exit(0); | |
} | |
static void Main(string[] args) | |
{ | |
////////// | |
// Init // | |
////////// | |
// Test if input arguments were supplied. | |
if (args.Length == 0) | |
{ | |
Console.WriteLine("Ran without any arguments, prompting for information instead. Run with /help for more info on which arguments are supported."); | |
} | |
// Convert arguments to list and string | |
List<String> argslist = new List<String>(); | |
string argsstring = ""; | |
for (int i = 0; i < args.Length; i++) | |
{ | |
argsstring = argsstring + " " + args[i]; | |
argslist.Add($"{args[i]}"); | |
} | |
// Check if argument is help | |
if (argslist.Contains("/help")) | |
{ | |
Console.WriteLine("You ran with /help."); | |
Program.Exit(); | |
} | |
// Check if url, entityid, username, and password exist | |
url = checkForArgs(argsstring, argslist, "/url", "URL", @"https*://\S+/httpinterface.aspx|\S+/"); | |
entityid = checkForArgs(argsstring, argslist, "/ent", "Entity ID", @"\d+"); | |
username = checkForArgs(argsstring, argslist, "/user", "Username", @"\w+"); | |
password = checkForArgs(argsstring, argslist, "/pass", "Password", @"\w+|"); | |
// Send login Request. | |
result = (Program.LoginRequest(url, entityid, username, password)); | |
sessionID = getSessionID(result); | |
// Write Current Session ID then logout. | |
Console.Write(sessionID); | |
Console.ReadLine(); | |
Program.Exit(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment