Thanks for the example. I had seen your example earlier and had tried
it and always get to one particular point where I cannot seem to get
beyond. There are two hidden fields both called web.fixed_value s that
appear to be something like a view state but the page is shtml. I am
and have been able to pull down the site, etc. but everytime I try and
post my data (with or without the web.fixed_value s) I always get the
response Method Not Allowed. Below is the code I am using along with
the sire I am trying to access with my account. ANy further help on
this would be greatly appreciated.
private void Page_Load(objec t sender, System.EventArg s e)
{
string LOGIN_URL = "http://augustachronicl e.com/login.shtml";
string cookieAge = "31536000";
try
{
HttpWebRequest webRequest = WebRequest.Crea te(LOGIN_URL) as
HttpWebRequest;
StreamReader responseReader = new
StreamReader(we bRequest.GetRes ponse().GetResp onseStream());
string responseData = responseReader. ReadToEnd();
responseReader. Close();
// get the web fixed values
string fixedvalue1 = ExtractFixedVal ues1(responseDa ta);
string fixedvalue2 = ExtractFixedVal ues2(responseDa ta);
string postData = String.Format(" web.fixed_value s={0}&web.fixed _values={1}&ACT ION=Login&USER= {2}&PASS={3}&co okie_age={4}",f ixedvalue1,fixe dvalue2,userNam e,
password, cookieAge);
// have a cookie container ready to receive the forms auth cookie
CookieContainer cookies = new CookieContainer ();
// now post to the login form
webRequest = WebRequest.Crea te(LOGIN_URL) as HttpWebRequest;
webRequest.Meth od = "POST";
webRequest.Cont entType = "applicatio n/x-www-form-urlencoded";
webRequest.Cook ieContainer = cookies;
// 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 it
issues
webRequest.GetR esponse().Close ();
// now we can send out cookie along with a request for the protected
page
webRequest = WebRequest.Crea te("http://augustachronicl e.com/stories/112404/usc_FBC--SpurrierProfile .shtml")
as HttpWebRequest;
webRequest.Cook ieContainer = cookies;
responseReader = new
StreamReader(we bRequest.GetRes ponse().GetResp onseStream());
// and read the response
responseData = responseReader. ReadToEnd();
responseReader. Close();
Response.Write( responseData);
}
catch (Exception ex)
{
Response.Write( ex.ToString());
}
}
private string ExtractFixedVal ues1(string s)
{
string viewStateNameDe limiter = "web.fixed_valu es";
string valueDelimiter = "value=\"";
int viewStateNamePo sition = s.IndexOf(viewS tateNameDelimit er);
int viewStateValueP osition = s.IndexOf(
valueDelimiter, viewStateNamePo sition
);
int viewStateStartP osition = viewStateValueP osition +
valueDelimiter. Length;
int viewStateEndPos ition = s.IndexOf("\"", viewStateStartP osition);
return HttpUtility.Url EncodeUnicode(
s.Substring(vie wStateStartPosi tion,
viewStateEndPos ition - viewStateStartP osition
)
);
}
private string ExtractFixedVal ues2(string s)
{
string viewStateNameDe limiter = "web.fixed_valu es";
string valueDelimiter = "value=\"";
int viewStateNamePo sition = s.IndexOf(viewS tateNameDelimit er);
int viewStateValueP osition = s.IndexOf(value Delimiter,
viewStateNamePo sition
);
int viewStateStartP osition = viewStateValueP osition +
valueDelimiter. Length;
int viewStateEndPos ition = s.IndexOf("\"", viewStateStartP osition);
string sTemp = s.Remove(0,view StateEndPositio n);
viewStateNamePo sition = sTemp.IndexOf(v iewStateNameDel imiter);
viewStateValueP osition = sTemp.IndexOf(
valueDelimiter, viewStateNamePo sition
);
viewStateStartP osition = viewStateValueP osition +
valueDelimiter. Length;
viewStateEndPos ition = sTemp.IndexOf(" \"", viewStateStartP osition);
return HttpUtility.Url EncodeUnicode(
sTemp.Substring (
viewStateStartP osition,
viewStateEndPos ition - viewStateStartP osition
)
);
}
Scott Allen <bitmask@[nospam].fred.net> wrote in message news:<k8******* *************** **********@4ax. com>...
Thanks, Joe. I appreciate the feedback.
--
Scott
http://www.OdeToCode.com/blogs/scott/
On Wed, 24 Nov 2004 20:48:24 -0500, "Joe Fallon"
<jf******@nospa mtwcny.rr.com> wrote:
Scott,
FYI - that was one of the best articles on the subject I ever read.
I was completely stuck on this issue about 6 months ago and I implemented it
straight away using the concepts you presented here.
Excellent work and explanation.