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

解决方案 »

  1.   

    this is a mistake of typing, but not my problem. i even can't get c when response.write c.
      

  2.   

    1.asp:
    <%
    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);
      }
    }
      

  3.   

    thanks to saucer.
    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.
      

  4.   

    no difference, seeRetrieving HTTP content in .NET
    http://www.west-wind.com/presentations/dotnetWebRequest/dotnetWebRequest.htm
      

  5.   

    i studied my problem more, things are like that:
    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?
      

  6.   

    on the client side, you should not use "set-cookie", use "cookie" header insteadshow your page code to see what is wrong
      

  7.   

    here is my code, i used it ok with asp server, but not ok with apcahe server.
    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();
    }
        }
      

  8.   

    hi, you got anything?
      

  9.   

    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>
      

  10.   

    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","");
        }
    }
      

  11.   

    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=/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>
      

  12.   

    try access;
    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.
      

  13.   

    I sent the postdata to that url, I got back a page similar to www.mysask.ca, what am I supposed to see?
      

  14.   

    you should get a list of company names, addresses, tels, etc. but i suppose you only see a index page.
      

  15.   

    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.
      

  16.   

    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?
      

  17.   

    truly weird, try to get a HTTP monitor to view the request HTTP header from the browser
      

  18.   

    what's a http monitor? how to get it?
      

  19.   

    thank you for today, saucer. i got to go. i have class in university this evening. hope to meet you tommorrow.
    thanks again.
    kevin
      

  20.   

    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", "");
        }
      

  21.   

    try
    http://www.naviscope.com/
    which monitors all http requests
      

  22.   

    ic, they must check you have a jsessionid firsthere 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=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
      

  23.   

    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.
      

  24.   

    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 exampleif (session.isNew())
    {
      response.sendRedirect("/portal/index.jsp");
    }basically, they want you to go to this index.jsp first
      

  25.   

    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.