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)website. 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 InitializeComponent() call is required for Windows Forms
designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new MainForm());
}
void Button1Click(object sender, System.EventArgs e)
{
this.textBox1.Text="Connecting...";
this.LOGIN_URL="http://loginpage"; this.SECRET_PAGE_URL
="http://targetpage";
this.USERNAME ="UserName";
this.PASSWORD ="Password";
HttpWebRequest webrequest=WebRequest.Create(LOGIN_URL) as
HttpWebRequest;
StreamReader responseReader=new
StreamReader(webrequest.GetResponse().GetResponseS tream());
string responseData = responseReader.ReadToEnd();
//this.textBox1.Text=responseData;
//extract PathInfo value and build our post data
string pathInfo=ExtractPathInfo(responseData);
MessageBox.Show(pathInfo,pathInfo);
//string
postData=String.Format("pathInfo={0}&username={1}& password={2}&Login=Login",
pathInfo, USERNAME, PASSWORD);
string
postData=String.Format("username={1}&password={2}& pathInfo={0}",
pathInfo, USERNAME, PASSWORD);
this.textBox1.Text=postData;
//have a cookie container ready to receive the forms auth cookie
CookieContainer cookies=new CookieContainer();
//now post to the login form
webrequest=WebRequest.Create(LOGIN_URL) as HttpWebRequest;
webrequest.Method="Post";
webrequest.Credentials = CredentialCache.DefaultCredentials;
webrequest.UserAgent="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.Accept="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.ContentType="application/x-www-form-urlencoded";
webrequest.AllowAutoRedirect=true;
webrequest.CookieContainer=cookies;
webrequest.Referer="http://unatime.merck.com/unatime/action/home";
//write the form values into the request message
StreamWriter requestWriter = new
StreamWriter(webrequest.GetRequestStream());
requestWriter.Write(postData);
requestWriter.Close();
//we don't need the contents of the response, just the cookie
try
{
webrequest.GetResponse().Close();
}
catch (WebException ee)
{
// MessageBox.Show(ee.Message);
// this.textBox1.Text=ee.Message;
}
//webrequest.GetResponse().Close();
//now we can send out cookie along with a request for the protected
page
webrequest = WebRequest.Create(SECRET_PAGE_URL) as HttpWebRequest;
webrequest.Method="Post";
webrequest.Credentials = CredentialCache.DefaultCredentials;
webrequest.UserAgent="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.Accept="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.ContentType="application/x-www-form-urlencoded";
webrequest.AllowAutoRedirect=true;
webrequest.CookieContainer=cookies;
webrequest.Referer="http://unatime.merck.com/unatime/action/home";
responseReader=new
StreamReader(webrequest.GetResponse().GetResponseS tream());
//StreamReader readStream = new StreamReader
(webrequest.GetResponse().GetResponseStream(), Encoding.UTF8);
//and read the response
responseData = responseReader.ReadToEnd();
responseReader.Close();
//Response.Write(responseData);
this.textBox1.Text=responseData;
}
private string ExtractPathInfo(string s)
{
string viewStateNameDelimiter="pathInfo";
string valueDelimiter="value=\"";
int viewStateNamePosition=s.IndexOf(viewStateNameDelim iter);
int viewStateValuePosition=s.IndexOf(valueDelimiter,
viewStateNamePosition);
int viewStateStartPosition=viewStateValuePosition +
valueDelimiter.Length;
//int viewStateEndPosition=s.IndexOf("\"", viewStateStartPosition);
int viewStateEndPosition=s.IndexOf("\"", viewStateStartPosition);
return
HttpUtility.UrlEncodeUnicode(s.Substring(viewState StartPosition,
viewStateEndPosition-viewStateStartPosition));
}
}
}