should be "c" instead of "s":response.write c & "=" & request.cookies(s)
===>
response.write c & "=" & request.cookies(c)you should use
Option Explicit
in your asp page to catch this kind of errors
===>
response.write c & "=" & request.cookies(c)you should use
Option Explicit
in your asp page to catch this kind of errors
<%
for each c in request.cookies
response.write c & "=" & request.cookies(c)
next
%>2.
testcookie.cs:using System;
using System.Net;
using System.Text;
using System.IO;class TestCookie
{
public static void Main()
{
String strURL = "http://localhost/csdn/1.asp";
HttpWebRequest webreq = (HttpWebRequest) WebRequest.Create(strURL);
webreq.CookieContainer = new CookieContainer();
webreq.CookieContainer.Add( new Cookie("name", "value", "/", "localhost"));
HttpWebResponse webres = (HttpWebResponse)webreq.GetResponse();
Stream respstream = webres.GetResponseStream();
StreamReader reader = new StreamReader(respstream,Encoding.GetEncoding("iso-8859-1"));
String strHTML = reader.ReadToEnd();
respstream.Close(); Console.Write(strHTML);
}
}
i have solved the problem. it's like that. i used post method. and set content length before setting cookies. so cookies are not transferred to server at all.
that's the real problem.
now, i got a new problem. i accessed a website with program with .net , program with mfc(wininet) and internet explorer. with the later two, i got no problem. but with .net, i cannot get the result page. why? is there any differences between them? maybe the site can identify different browser?
do you have any experience on that?
thank you.
http://www.west-wind.com/presentations/dotnetWebRequest/dotnetWebRequest.htm
first, i send a request (post) to server, server(Apache) return a cookie(jspidxxxxx=xxxxxxxxxxx) to me, and leaded the program to antoher page(get). so i followed it with cookie set. then something was wrong with this page. the page should display something. but it didn't. it leaded me to an error page(no error information).do you have experience about set-cookie2. or how apache manage session?
public void getPage(String url, String postData)
{
//request 1
HttpWebRequest req;
HttpWebResponse resp; string theUrl = url;
string thePostData = postData;
CookieCollection cookies = null; while(theUrl!="")
{
req = (HttpWebRequest)WebRequest.Create(theUrl);
req.CookieContainer = new CookieContainer(); if(cookies!=null)
req.CookieContainer.Add(cookies);
//req.CookieContainer.Add( new Cookie("myname","kevin","/","localhost") ); req.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; .NET CLR 1.0.2914)";
req.AllowAutoRedirect = false;
req.Timeout = 300000;
req.KeepAlive = true; if(thePostData!="")
{
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] PostData = System.Text.Encoding.ASCII.GetBytes(thePostData);
req.ContentLength = PostData.Length;
Stream tempStream = req.GetRequestStream();
tempStream.Write(PostData,0,PostData.Length);
tempStream.Close();
}
else
req.Method = "GET";
//get response
resp = (HttpWebResponse)req.GetResponse();
//get headers
foreach(string ss in resp.Headers)
{
string m = resp.Headers[ss];
m += "";
} //transfer to
theUrl = resp.Headers["Location"] + ""; thePostData = "";
//cookies
if(resp.Cookies.Count!=0)
cookies = resp.Cookies;
else
cookies = null; //write page content
Stream ReceiveStream = resp.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader sr = new StreamReader( ReceiveStream, encode );
string s = sr.ReadToEnd();
StreamWriter writer = File.CreateText(@"2.htm");
writer.WriteLine (s);
writer.Close();
resp.Close();
}
}
<%
Cookie cookie = new Cookie("abc","123");;
response.addCookie(cookie); String redirectURL = "testplus.jsp";
response.sendRedirect(redirectURL);
%>
2. testplus.jsp:
<%@ page errorPage="errorpage.jsp" %>
<%=request.getParameter("plus")%><BR>
JSPID:<%=session.getId()%><BR>
Cookies:<BR>
<%
Cookie cookies[] = request.getCookies();
for (int i=0; i < cookies.length; i++)
{
%>
<%=cookies[i].getName()%>=<%=cookies[i].getValue()%>
<BR>
<%
}%>
<form method="post">
<input type="text" name="plus" value="+">
<input type="submit" onclick="this.form.plus.value = encodeURIComponent(this.form.plus.value); alert(this.form.plus.value)">
</form>
<%
int i=1/0;
%>3. errorpage.jsp:
<%@ page isErrorPage="true" %>
<html>
<body bgcolor="red">
<h1> The exception <%= exception.getMessage() %> tells me you
made a wrong choice.
</body>
</html>
using System.Net;
using System.IO;
using System.Text;class GetPage
{
public static void getPage(String url, String postData)
{
//request 1
HttpWebRequest req;
HttpWebResponse resp; string theUrl = url;
string thePostData = postData;
CookieCollection cookies = null; while(theUrl!="")
{
req = (HttpWebRequest)WebRequest.Create(theUrl);
req.CookieContainer = new CookieContainer(); if(cookies!=null)
req.CookieContainer.Add(cookies);
//req.CookieContainer.Add( new Cookie("myname","kevin","/","localhost") ); req.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; .NET CLR 1.0.2914)";
req.AllowAutoRedirect = false;
req.Timeout = 300000;
req.KeepAlive = true; if(thePostData!="")
{
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] PostData = System.Text.Encoding.ASCII.GetBytes(thePostData);
req.ContentLength = PostData.Length;
Stream tempStream = req.GetRequestStream();
tempStream.Write(PostData,0,PostData.Length);
tempStream.Close();
}
else
req.Method = "GET";
//get response
resp = (HttpWebResponse)req.GetResponse(); Console.WriteLine("current url:" + theUrl); //get headers
Console.WriteLine("headers:");
StringBuilder m = new StringBuilder();;
foreach(string ss in resp.Headers)
{
m.Append(ss+"="+resp.Headers[ss]);
m.Append("\n");
}
Console.WriteLine(m.ToString()); //transfer to
theUrl = resp.Headers["Location"] + "";
Console.WriteLine("redirection url:" + theUrl);
thePostData = "";
//cookies
Console.WriteLine("cookies:" + resp.Headers["Set-Cookie"]); if(resp.Cookies.Count!=0)
cookies = resp.Cookies;
else
cookies = null; //write page content
Stream ReceiveStream = resp.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader sr = new StreamReader( ReceiveStream, encode );
string s = sr.ReadToEnd();
//StreamWriter writer = File.CreateText(@"2.htm");
//writer.WriteLine (s);
//writer.Close(); Console.WriteLine("page content:");
Console.WriteLine(s);
Console.WriteLine("\n");
resp.Close();
}
}
public static void Main()
{ getPage("http://localhost:8080/examples/jsp/redirect.jsp","");
}
}
current url:http://localhost:8080/examples/jsp/redirect.jsp
headers:
Content-Type=text/html;charset=ISO-8859-1
Date=Mon, 16 Sep 2002 20:20:35 GMT
Transfer-Encoding=chunked
Location=http://localhost:8080/examples/jsp/testplus.jsp
Server=Apache Tomcat/4.0-rc1 (HTTP/1.1 Connector)
Set-Cookie=abc=123,JSESSIONID=F215261203D391932685B3218534A726;Path=/examplesredirection url:http://localhost:8080/examples/jsp/testplus.jsp
cookies:abc=123,JSESSIONID=F215261203D391932685B3218534A726;Path=/examples
page content:
current url:http://localhost:8080/examples/jsp/testplus.jsp
headers:
Content-Type=text/html;charset=ISO-8859-1
Date=Mon, 16 Sep 2002 20:20:35 GMT
Transfer-Encoding=chunked
Server=Apache Tomcat/4.0-rc1 (HTTP/1.1 Connector)redirection url:
cookies:
page content:<html>
<body bgcolor="red">
<h1> The exception / by zero tells me you
made a wrong choice.
</body>
</html>
http://mysask.sympatico.ca/portal/generic_process.jsp?beanID=734&viewID=process_yellow_pageswith postdataMEM=800&FUNC=FORMATSEARCH&L=&C=toys&DIR=&AT=SKyou can find the result page by using ie in www.mysask.ca. fill in the form and submit it.
why?
thanks again.
kevin
it's like this: private static CookieCollection cs = null;
public static void Main()
{
cs = getPage("http://mysask.sympatico.ca/portal/index.jsp","");
//getPage("http://mysask.sympatico.ca/portal/generic_process.jsp?beanID=734&viewID=process_yellow_pages", "MEM=800&FUNC=FORMATSEARCH&L=&C=toys&DIR=&AT=SK");
getPage("http://mysask.sympatico.ca/portal/generic.jsp?beanID=734&viewID=result_yellow_pages&jsessionid=3322971032209581285", "");
}
http://www.naviscope.com/
which monitors all http requests
POST http://mysask.sympatico.ca/portal/generic_process.jsp?beanID=734&viewID=process_yellow_pages HTTP/1.0
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*
Referer: http://mysask.sympatico.ca/portal/index.jsp
Accept-Language: en-us
Content-Type: application/x-www-form-urlencoded
Proxy-Connection: Keep-Alive
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461; .NET CLR 1.0.3705)
Host: mysask.sympatico.ca
Content-Length: 149
Pragma: no-cache
Cookie: jsessionid=2445841032211906064MEM=800&FUNC=FORMATSEARCH&action2=homeSearch&business=&city=&provinceId=&categorySym=&searchType=yellow_pages&L=&C=toys&DIR=&AT=SK&FIND.x=8&FIND.y=12
{
response.sendRedirect("/portal/index.jsp");
}basically, they want you to go to this index.jsp first