Home
%3CLINGO-SUB%20id%3D%22lingo-sub-287838%22%20slang%3D%22en-US%22%3EHow%20to%20call%20a%20SSL%20web%20page%20programmatically%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-287838%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Jan%2016%2C%202014%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3ENot%20all%20days%20are%20equal.%26nbsp%3BRecently%2C%20my%20day%20was%26nbsp%3Bchallenged%20with%20a%20fun%20but%20daunting%20task%3A%20to%20assist%20a%20customer%20to%20write%20some%20C%23%20code%20to%20call%20a%26nbsp%3Bhttps%20webpage%20programmatically.%20Here%20is%20what%20we%20ended%20up%20coding%20the%20login%20page%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENote%3A%20Since%20every%20site%20is%20different%2C%20you%20will%20need%20to%20collect%26nbsp%3BFiddler%20trace%20to%20intercept%20the%20form%20data%20and%20code%20them%20accordingly%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Epublic%20partial%20class%20Form1%20%3A%20Form%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%20%3CBR%20%2F%3ECookieContainer%20cookieJar%20%3D%20null%3B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Epublic%20Form1()%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3EInitializeComponent()%3B%20%3CBR%20%2F%3EcookieJar%20%3D%20new%20CookieContainer()%3B%20%3CBR%20%2F%3E%7D%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Eprivate%20void%20button1_Click(object%20sender%2C%20EventArgs%20e)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%2F%2FPOST%20form%20data%20to%20authenticate%20the%20session%20%3CBR%20%2F%3EMakeHttpCall(%22POST%22%2C%22%20%3CA%20href%3D%22https%3A%2F%2Fwww.mysite.com%2Flogin%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.mysite.com%2Flogin%20%3C%2FA%3E%22)%3B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7D%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Eprivate%20void%20MakeHttpCall(string%20method%2C%20string%20uri)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3EHttpWebRequest%20webReq%20%3D%20(HttpWebRequest)WebRequest.Create(uri)%3B%20%3CBR%20%2F%3EwebReq.CookieContainer%20%3D%20cookieJar%3B%20%3CBR%20%2F%3EwebReq.Method%20%3D%20method%3B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Eif(method%20%3D%3D%20%22POST%22)%7B%20%3CBR%20%2F%3E%2F%2Fhave%20to%20set%20the%20POST%20data%20content%20type%20%3CBR%20%2F%3EwebReq.ContentType%20%3D%20%22application%2Fx-www-form-urlencoded%22%3B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%2F%2Fcollect%20the%20formData%20from%20Fiddler%20trace%20%3CBR%20%2F%3Estring%20formData%20%3CA%20href%3D%22mailto%3A%3D%40%26quot%3Butf8%3D%25E2%259C%2593%26amp%3Bauthenticity_token%3D%252Btw4XXBXYomXpy5kDswLQNnGae04w%252Fb0IeGZRMGqjnw%253D%26amp%3Buser%255Blogin%255D%3Dmyname%26amp%3Buser%255Bpassword%255D%3DJ*%40pw%252F%257E%257C%257C%257C%26amp%3Bcommit%3DLogin%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3D%40%22utf8%3D%25E2%259C%2593%26amp%3Bauthenticity_token%3D%252AAbgakfjdADFOEIRbereoidfnfidfjdfj%252Fb0IeGZRMGqjnw%253D%26amp%3Buser%255Blogin%255D%3Dmyname%26amp%3Buser%255Bpassword%255D%3DJ*%40pw%252F%257E%257C%257C%257C%26amp%3Bcommit%3DLogin%20%3C%2FA%3E%22%3B%20%3CBR%20%2F%3EwebReq.Referer%20%3D%20%22%20%3CA%20href%3D%22https%3A%2F%2Fwww.mysite.com%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.mysite.com%2F%20%3C%2FA%3E%22%3B%20%3CBR%20%2F%3E%2F%2F%20we%20need%20to%20store%20the%20data%20into%20a%20byte%20array.%20Content%20will%20be%20what%20server%20looking%20for%20%3CBR%20%2F%3Ebyte%5B%5D%20PostData%20%3D%20System.Text.Encoding.ASCII.GetBytes(formData)%3B%20%3CBR%20%2F%3EwebReq.ContentLength%20%3D%20PostData.Length%3B%20%3CBR%20%2F%3EStream%20tempStream%20%3D%20webReq.GetRequestStream()%3B%20%3CBR%20%2F%3E%2F%2F%20write%20the%20data%20to%20be%20posted%20to%20the%20Request%20Stream%20%3CBR%20%2F%3EtempStream.Write(PostData%2C0%2CPostData.Length)%3B%20%3CBR%20%2F%3EtempStream.Close()%3B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7D%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%2F%2FGet%20the%20response%20from%20the%20server%20%3CBR%20%2F%3EHttpWebResponse%20webResp%20%3D%20(HttpWebResponse)%20webReq.GetResponse()%3B%20%3CBR%20%2F%3EStreamReader%20sr%20%3D%20new%20StreamReader(webResp.GetResponseStream()%2C%20Encoding.ASCII)%3B%20%3CBR%20%2F%3E%2F%2Fput%20the%20stream%20data%20in%20a%20string%20%3CBR%20%2F%3EString%20respData%20%3D%20sr.ReadToEnd()%3B%20%3CBR%20%2F%3Esr.Close()%3B%20%3CBR%20%2F%3EwebResp.Close()%3B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Eif%20(method%20%3D%3D%20%22POST%22)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3EMessageBox.Show(respData.ToString())%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%7D%3C%2FP%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-287838%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Jan%2016%2C%202014%20Not%20all%20days%20are%20equal.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-287838%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EC%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Ehttpwebrequest%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Ehttpwebresponse%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESSL%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft
First published on MSDN on Jan 16, 2014

Not all days are equal. Recently, my day was challenged with a fun but daunting task: to assist a customer to write some C# code to call a https webpage programmatically. Here is what we ended up coding the login page:


Note: Since every site is different, you will need to collect Fiddler trace to intercept the form data and code them accordingly


public partial class Form1 : Form


{
CookieContainer cookieJar = null;


public Form1()
{
InitializeComponent();
cookieJar = new CookieContainer();
}


private void button1_Click(object sender, EventArgs e)
{
//POST form data to authenticate the session
MakeHttpCall("POST"," https://www.mysite.com/login ");


}


private void MakeHttpCall(string method, string uri)
{
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(uri);
webReq.CookieContainer = cookieJar;
webReq.Method = method;


if(method == "POST"){
//have to set the POST data content type
webReq.ContentType = "application/x-www-form-urlencoded";


//collect the formData from Fiddler trace
string formData =@"utf8=%E2%9C%93&authenticity_token=%2AAbgakfjdADFOEIRbereoidfnfidfjdfj%2Fb0IeGZRMGqjnw%3D&use... ";
webReq.Referer = " https://www.mysite.com/ ";
// we need to store the data into a byte array. Content will be what server looking for
byte[] PostData = System.Text.Encoding.ASCII.GetBytes(formData);
webReq.ContentLength = PostData.Length;
Stream tempStream = webReq.GetRequestStream();
// write the data to be posted to the Request Stream
tempStream.Write(PostData,0,PostData.Length);
tempStream.Close();


}


//Get the response from the server
HttpWebResponse webResp = (HttpWebResponse) webReq.GetResponse();
StreamReader sr = new StreamReader(webResp.GetResponseStream(), Encoding.ASCII);
//put the stream data in a string
String respData = sr.ReadToEnd();
sr.Close();
webResp.Close();


if (method == "POST")
{
MessageBox.Show(respData.ToString());
}
}
}