using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.Net.Security;
using System.IO;
using System.Web;
namespace rtspost
{
class Program
{
static void Main(string[] args)
{
try
{
Http http = new Http();
string url = "http://127.0.0.1:24780/ProgramStore/test.cep";
string bodyFilename = "body.txt";
string headerFilename = "header.txt";
string responseFilename = "response.txt";
string responseHeaderFilename = "responseheader.txt";
byte[] bodydata = new byte[0];
string headerdata = "";
byte[] responsedata = new byte[0];
string responseheaderdata = "";
if (args.Length == 0)
{
System.Console.WriteLine("No parameter, running with these parameters:");
System.Console.WriteLine("rtspost.exe -u http://127.0.0.1:24780/ProgramStore/test.cep" + " -b body.txt -h header.txt -r response.txt -rh responseheader.txt");
}
int i = 0;
while ((i+1) < args.Length)
{
switch (args[i].ToLower())
{
case "-u":
url = args[i + 1];
break;
case "-b":
bodyFilename = args[i + 1];
break;
case "-h":
headerFilename = args[i + 1];
break;
case "-r":
responseFilename = args[i + 1];
break;
case "-rh":
responseHeaderFilename = args[i + 1];
break;
default: break;
}
i += 2;
}
if (File.Exists(bodyFilename))
bodydata = System.IO.File.ReadAllBytes(bodyFilename);
if (File.Exists(headerFilename))
headerdata = System.IO.File.ReadAllText(headerFilename, System.Text.Encoding.ASCII);
if (File.Exists(responseFilename))
File.Delete(responseFilename);
if (File.Exists(responseHeaderFilename))
File.Delete(responseHeaderFilename);
if (http.Post(url, false, bodydata, headerdata, ref responsedata, ref responseheaderdata))
{
File.WriteAllBytes(responseFilename, responsedata);
System.Console.WriteLine("Wrote reponse to: " + responseFilename);
File.WriteAllText(responseHeaderFilename, responseheaderdata, System.Text.Encoding.ASCII);
System.Console.WriteLine("Wrote reponse header to: " + responseHeaderFilename);
}
}
catch (Exception e)
{
System.Console.WriteLine("Exception e: " + e.Message);
}
}
}
public class Http
{
NetworkCredential credentials;
X509Certificate2 certificate;
HttpWebRequest httpWebRequest;
public bool Post(string URIString, bool TSLEncryption, byte[] bodydata, string header, ref byte[] responseData, ref string responseheader)
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(URIString));
AddHeaderToRequest(header);
httpWebRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
httpWebRequest.Timeout = 10000; //In milliseconds
if (this.certificate != null)
{
httpWebRequest.ClientCertificates = new X509CertificateCollection();
httpWebRequest.ClientCertificates.Add(certificate);
}
if (this.credentials != null)
{
httpWebRequest.Credentials = this.credentials;
}
// Add some data that will go into headers
httpWebRequest.ContentType = "text/plain";
if (bodydata != null && bodydata.Length > 0)
{
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = bodydata.Length;
Stream requestStream = httpWebRequest.GetRequestStream();
requestStream.Write(bodydata, 0, bodydata.Length);
requestStream.Close();
}
else
{
httpWebRequest.Method = "GET";
}
System.Net.HttpWebResponse resp = httpWebRequest.GetResponse() as HttpWebResponse;
if (resp == null)
return false;
using (MemoryStream ms = new MemoryStream())
{
resp.GetResponseStream().CopyTo(ms);
responseData = ms.ToArray();
}
responseheader = resp.Headers.ToString();
return true;
}
public bool ClientCertificate(string issuedBy, string serialNr, string certStoreName, int certStoreLocation)
{
StoreLocation location = StoreLocation.CurrentUser;
if (certStoreLocation == 0)
location = StoreLocation.LocalMachine;
X509Store store = new X509Store(certStoreName, location);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindBySerialNumber, serialNr, false);
this.certificate = fcollection[0];
return true;
}
public bool UseAuthentication(string username, string password)
{
if (username != null)
{
password = (password == null) ? "" : password;
this.credentials = new NetworkCredential(username, password);
return true;
}
return false;
}
private void AddHeaderToRequest(string headerData)
{
string[] headerSplit = headerData.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string header in headerSplit)
{
int separator = header.IndexOf(':');
string headerName = header.Substring(0, separator);
separator++;
string headerValue = header.Substring(separator, header.Length - separator).TrimStart();
switch (headerName.ToLower()) // HTTP protocol allows both upper and lower case so lower before compare
{
case "accept":
httpWebRequest.Accept = headerValue;
break;
case "connection":
httpWebRequest.Connection = headerValue;
break;
case "content-length":
httpWebRequest.ContentLength = System.Int64.Parse(headerValue);
break;
case "content-type":
httpWebRequest.ContentType = headerValue;
break;
case "date":
// "Set by the system to current date."
// request.Headers.Add(header);
break;
case "expect":
if (headerValue == "<NOTHING>")
{
System.Net.ServicePointManager.Expect100Continue = false;
break;
}
else
System.Net.ServicePointManager.Expect100Continue = true;
httpWebRequest.Expect = headerValue;
break;
case "host":
// "Set by the system to current host information."
// request.Headers.Add(header);
break;
case "if-modified-since":
httpWebRequest.IfModifiedSince = System.DateTime.Parse(headerValue);
break;
case "range":
// if this header should be supported either parse header value or change GUI to
// give proper input: stringSpecifier, integerStart, integerEnd
// request.AddRange();
// request.Headers.Add(header);
break;
case "referer":
httpWebRequest.Referer = headerValue;
break;
case "te":
httpWebRequest.TransferEncoding = headerValue;
break;
case "user-agent":
httpWebRequest.UserAgent = headerValue;
break;
default:
httpWebRequest.Headers.Add(header);
break;
}
}
}
}
}
0 Comments