Created
July 11, 2014 13:07
-
-
Save CipherLab/5d5ea7faffbdf6ab44e3 to your computer and use it in GitHub Desktop.
Upload / Download via HTTPS
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.Generic; | |
using System.Collections.Specialized; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
public class HTTPClient | |
{ | |
private string url = null; | |
private string username = null; | |
private string password = null; | |
/* Construct Object */ | |
public HTTPClient( | |
string hostIP, | |
string userName, | |
string Password) | |
{ | |
url = hostIP; | |
username = userName; | |
password = Password; | |
} | |
public void LoginAndUpload(string localfile) | |
{ | |
WebClient wc = new WebClient(); | |
using (var client = new CookieAwareWebClient()) | |
{ | |
var values = new NameValueCollection | |
{ | |
{ "start-url", @"/~" }, | |
{ "user", username }, | |
{ "password", password }, | |
{ "switch", "Log In" }, | |
}; | |
client.UploadValues("https://sdfiltran.adp.com/login", values); | |
// If the previous call succeeded we now have a valid authentication cookie | |
// so we could download the protected page | |
var upload_values = new NameValueCollection | |
{ | |
{ "SUBMIT", "Upload File" }, | |
}; | |
// HttpUploadFile("https://sdfiltran.adp.com/?T", @"d:\AEGISDOWNLOAD.csv", "file", "application/vnd.ms-excel", upload_values, client.CookieContainer); | |
HttpUploadFile(url + "/?T", localfile, "file", "text/html", upload_values, client.CookieContainer); | |
} | |
//wc.UploadFile(url, "post", file); | |
} | |
public void LoginAndDownload(string remote_file, string local_file) | |
{ | |
try | |
{ | |
WebClient wc = new WebClient(); | |
using (var client = new CookieAwareWebClient()) | |
{ | |
var values = new NameValueCollection | |
{ | |
{ "start-url", @"/~" }, | |
//{ "user", "yt4_etime" }, | |
//{ "password", "Aegis-66x" }, | |
{ "user", username }, | |
{ "password", password }, | |
{ "switch", "Log In" }, | |
}; | |
client.UploadValues("https://sdfiltran.adp.com/login", values); | |
// If the previous call succeeded we now have a valid authentication cookie | |
// so we could download the protected page | |
//client.DownloadFile("https://sdfiltran.adp.com/aegisimport.csv?B", @"d:\AEGISDOWNLOAD.csv"); | |
client.DownloadFile(url + @"/" + remote_file + "?B", local_file); | |
int i = 0; | |
} | |
} | |
catch (Exception ex) | |
{ | |
} | |
} | |
private class CookieAwareWebClient : WebClient | |
{ | |
public CookieAwareWebClient() | |
: this(new CookieContainer()) | |
{ } | |
public CookieAwareWebClient(CookieContainer c) | |
{ | |
this.CookieContainer = c; | |
} | |
public CookieContainer CookieContainer { get; set; } | |
protected override WebRequest GetWebRequest(Uri address) | |
{ | |
WebRequest request = base.GetWebRequest(address); | |
var castRequest = request as HttpWebRequest; | |
if (castRequest != null) | |
{ | |
castRequest.CookieContainer = this.CookieContainer; | |
} | |
return request; | |
} | |
} | |
public void HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc, CookieContainer cookies) | |
{ | |
string user = "yt4_etime"; | |
string password = "Aegis-66x"; | |
Debug.WriteLine(string.Format("Uploading {0} to {1}", file, url)); | |
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); | |
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); | |
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url); | |
wr.ContentType = "multipart/form-data; boundary=" + boundary; | |
wr.Method = "POST"; | |
wr.KeepAlive = true; | |
wr.CookieContainer = cookies; | |
//wr.PreAuthenticate = true; | |
//wr.Credentials = new NetworkCredential(user, password, "https://sdfiltran.adp.com"); | |
//using (StreamWriter writer = new StreamWriter(wr.GetRequestStream(), Encoding.ASCII)) | |
//{ | |
// writer.Write("user=" + user + "&password=" + password); | |
//} | |
Stream rs = wr.GetRequestStream(); | |
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\r{1}"; | |
foreach (string key in nvc.Keys) | |
{ | |
rs.Write(boundarybytes, 0, boundarybytes.Length); | |
string formitem = string.Format(formdataTemplate, key, nvc[key]); | |
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem); | |
rs.Write(formitembytes, 0, formitembytes.Length); | |
} | |
rs.Write(boundarybytes, 0, boundarybytes.Length); | |
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\r"; | |
string header = string.Format(headerTemplate, paramName, file, contentType); | |
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header); | |
rs.Write(headerbytes, 0, headerbytes.Length); | |
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read); | |
byte[] buffer = new byte[4096]; | |
int bytesRead = 0; | |
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) | |
{ | |
rs.Write(buffer, 0, bytesRead); | |
} | |
fileStream.Close(); | |
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r--" + boundary + "--\r"); | |
rs.Write(trailer, 0, trailer.Length); | |
rs.Close(); | |
WebResponse wresp = null; | |
try | |
{ | |
wresp = wr.GetResponse(); | |
Stream stream2 = wresp.GetResponseStream(); | |
StreamReader reader2 = new StreamReader(stream2); | |
Debug.WriteLine(string.Format("File uploaded, server response is: {0}", reader2.ReadToEnd())); | |
} | |
catch (Exception ex) | |
{ | |
Debug.WriteLine("Error uploading file", ex); | |
if (wresp != null) | |
{ | |
wresp.Close(); | |
wresp = null; | |
} | |
} | |
finally | |
{ | |
wr = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment