Cookie: why i can't use cookie?

qinglinmeng 2002-09-15 01:55:02
My program is like this:

//client (c#)
HttpWebRequest req = WebRequest.Create("http://localhost/1.asp");
req.CookieContainer.Add( new Cookie(
"name", "value", "/", "localhost"
));

//send request and get response

//server (asp)
for each c in request.cookies
response.write c & "=" & request.cookies(s)
next

but the cookie i set in client code can not be displayed, why?
...全文
113 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
qinglinmeng 2002-09-18
  • 打赏
  • 举报
回复
fini.
qinglinmeng 2002-09-17
  • 打赏
  • 举报
回复
yes, you are exactly right as always. i firstly accessed index.jsp. get a cookie for it, and use it to access generic_process.jsp. and gto the page i need.

woooo!!!

thank u, saucer. you did greatly helped me in recent days. i owe you a dinner. if you come to Montreal, don't forget give me an email. i will be host.

let me finish my work before finish this discussion.


saucer 2002-09-17
  • 打赏
  • 举报
回复
what I meant is, in their generic_process.jsp, they check if the session is new, if true, they redirect the page back to the index.jsp, for example

if (session.isNew())
{
response.sendRedirect("/portal/index.jsp");
}

basically, they want you to go to this index.jsp first
qinglinmeng 2002-09-17
  • 打赏
  • 举报
回复
i know what you mean: there is an important session operation in index.jsp. i have to access it, get cookie from it before folliwng operation.

let me try it.
zsw5337 2002-09-17
  • 打赏
  • 举报
回复
up
saucer 2002-09-17
  • 打赏
  • 举报
回复
ic, they must check you have a jsessionid first

here is the Header the browser sent to the server:
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=2445841032211906064

MEM=800&FUNC=FORMATSEARCH&action2=homeSearch&business=&city=&provinceId=&categorySym=&searchType=yellow_pages&L=&C=toys&DIR=&AT=SK&FIND.x=8&FIND.y=12

saucer 2002-09-17
  • 打赏
  • 举报
回复
try
http://www.naviscope.com/
which monitors all http requests
qinglinmeng 2002-09-17
  • 打赏
  • 举报
回复
by the way, i got result like this. first, access index.jsp to get a cookie. then call getPage again with the cookie. i got the result.
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", "");
}
qinglinmeng 2002-09-17
  • 打赏
  • 举报
回复
thank you for today, saucer. i got to go. i have class in university this evening. hope to meet you tommorrow.
thanks again.
kevin
qinglinmeng 2002-09-17
  • 打赏
  • 举报
回复
what's a http monitor? how to get it?
saucer 2002-09-17
  • 打赏
  • 举报
回复
truly weird, try to get a HTTP monitor to view the request HTTP header from the browser
qinglinmeng 2002-09-17
  • 打赏
  • 举报
回复
that's my problem. i should have get a result page. and my code works fine in other site. including your localhost, my localhost, but it works bad in mysask.com.
why?
qinglinmeng 2002-09-17
  • 打赏
  • 举报
回复
if you fill in the form on the left side of the www.mysask.ca. it is about seach by name or by category, input "toys" in category and leave others in default. you will get the page that you suppoesed to have.
qinglinmeng 2002-09-17
  • 打赏
  • 举报
回复
you should get a list of company names, addresses, tels, etc. but i suppose you only see a index page.
saucer 2002-09-17
  • 打赏
  • 举报
回复
I sent the postdata to that url, I got back a page similar to www.mysask.ca, what am I supposed to see?
qinglinmeng 2002-09-17
  • 打赏
  • 举报
回复
try access;
http://mysask.sympatico.ca/portal/generic_process.jsp?beanID=734&viewID=process_yellow_pages

with postdata

MEM=800&FUNC=FORMATSEARCH&L=&C=toys&DIR=&AT=SK

you can find the result page by using ie in www.mysask.ca. fill in the form and submit it.
saucer 2002-09-17
  • 打赏
  • 举报
回复
the output is like this:

E:\>GetPage
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=/examples

redirection 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>
saucer 2002-09-17
  • 打赏
  • 举报
回复
using System;
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","");
}
}
saucer 2002-09-17
  • 打赏
  • 举报
回复
actually, I do not even know what your problem is, here is what I did with a console program:

1. redirect.jsp:
<%
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>
qinglinmeng 2002-09-17
  • 打赏
  • 举报
回复
hi, you got anything?
加载更多回复(10)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧