Hi All,
I have a very small screen scrape application, that has a small
problem. when I run the app and I have fiddler(an http tool to view
what is being sent by the requests/responses,
http://www.fiddlertool.com) the app works, and I am able to login to
the (intranet)websi te. If do not run the app while fiddler is running,
it does not work(the app returns html of the login page, instead of the
target page).
Here is the code, thanks in advance
Note it maybe easier to copy and paste this code into notepad to
view....
/*
* User: Mccollid
* Date: 10/3/2005
* Time: 11:25 AM
*
*/
using System;
using System.Drawing;
using System.Windows. Forms;
using System.Net;
using System.IO;
using System.Text;
using System.Web;
namespace ScreenScraper
{
/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : System.Windows. Forms.Form
{
private System.Windows. Forms.Button button1;
private System.Windows. Forms.TextBox textBox1;
private string LOGIN_URL;
private string USERNAME;
private string PASSWORD;
private string SECRET_PAGE_URL ;
private string COOKIEHOLDER;
public MainForm()
{
//
// The InitializeCompo nent() call is required for Windows Forms
designer support.
//
InitializeCompo nent();
//
// TODO: Add constructor code after the InitializeCompo nent() call.
//
}
[STAThread]
public static void Main(string[] args)
{
Application.Run (new MainForm());
}
void Button1Click(ob ject sender, System.EventArg s e)
{
this.textBox1.T ext="Connecting ...";
this.LOGIN_URL= "http://loginpage"; this.SECRET_PAG E_URL
="http://targetpage";
this.USERNAME ="UserName";
this.PASSWORD ="Password";
HttpWebRequest webrequest=WebR equest.Create(L OGIN_URL) as
HttpWebRequest;
StreamReader responseReader= new
StreamReader(we brequest.GetRes ponse().GetResp onseStream());
string responseData = responseReader. ReadToEnd();
//this.textBox1.T ext=responseDat a;
//extract PathInfo value and build our post data
string pathInfo=Extrac tPathInfo(respo nseData);
MessageBox.Show (pathInfo,pathI nfo);
//string
postData=String .Format("pathIn fo={0}&username ={1}&password={ 2}&Login=Login" ,
pathInfo, USERNAME, PASSWORD);
string
postData=String .Format("userna me={1}&password ={2}&pathInfo={ 0}",
pathInfo, USERNAME, PASSWORD);
this.textBox1.T ext=postData;
//have a cookie container ready to receive the forms auth cookie
CookieContainer cookies=new CookieContainer ();
//now post to the login form
webrequest=WebR equest.Create(L OGIN_URL) as HttpWebRequest;
webrequest.Meth od="Post";
webrequest.Cred entials = CredentialCache .DefaultCredent ials;
webrequest.User Agent="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 1.0.3705)";
webrequest.Acce pt="Accept: image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel,
application/vnd.ms-powerpoint, application/msword, */*";
webrequest.Cont entType="applic ation/x-www-form-urlencoded";
webrequest.Allo wAutoRedirect=t rue;
webrequest.Cook ieContainer=coo kies;
webrequest.Refe rer="http://unatime.merck.c om/unatime/action/home";
//write the form values into the request message
StreamWriter requestWriter = new
StreamWriter(we brequest.GetReq uestStream());
requestWriter.W rite(postData);
requestWriter.C lose();
//we don't need the contents of the response, just the cookie
try
{
webrequest.GetR esponse().Close ();
}
catch (WebException ee)
{
// MessageBox.Show (ee.Message);
// this.textBox1.T ext=ee.Message;
}
//webrequest.GetR esponse().Close ();
//now we can send out cookie along with a request for the protected
page
webrequest = WebRequest.Crea te(SECRET_PAGE_ URL) as HttpWebRequest;
webrequest.Meth od="Post";
webrequest.Cred entials = CredentialCache .DefaultCredent ials;
webrequest.User Agent="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 1.0.3705)";
webrequest.Acce pt="Accept: image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel,
application/vnd.ms-powerpoint, application/msword, */*";
webrequest.Cont entType="applic ation/x-www-form-urlencoded";
webrequest.Allo wAutoRedirect=t rue;
webrequest.Cook ieContainer=coo kies;
webrequest.Refe rer="http://unatime.merck.c om/unatime/action/home";
responseReader= new
StreamReader(we brequest.GetRes ponse().GetResp onseStream());
//StreamReader readStream = new StreamReader
(webrequest.Get Response().GetR esponseStream() , Encoding.UTF8);
//and read the response
responseData = responseReader. ReadToEnd();
responseReader. Close();
//Response.Write( responseData);
this.textBox1.T ext=responseDat a;
}
private string ExtractPathInfo (string s)
{
string viewStateNameDe limiter="pathIn fo";
string valueDelimiter= "value=\"";
int viewStateNamePo sition=s.IndexO f(viewStateName Delimiter);
int viewStateValueP osition=s.Index Of(valueDelimit er,
viewStateNamePo sition);
int viewStateStartP osition=viewSta teValuePosition +
valueDelimiter. Length;
//int viewStateEndPos ition=s.IndexOf ("\"", viewStateStartP osition);
int viewStateEndPos ition=s.IndexOf ("\"", viewStateStartP osition);
return
HttpUtility.Url EncodeUnicode(s .Substring(view StateStartPosit ion,
viewStateEndPos ition-viewStateStartP osition));
}
}
}