2011-11-17 4 views
0

이 나는 ​​웹 페이지를 긁어 할 노력하고있어하지만 데이터를 게시하기 위해 나는쿠키에서 웹 세션을 얻는 방법?

은 어떻게 ID 것을 얻을 수 web_session = HQJ3G1GPAAHRZGFR

같은 웹 세션 ID가 필요?

내 코드는 지금까지 있습니다 :

Private Sub test() 

    Dim postData As String = "web_session=HQJ3G1GPAAHRZGFR&intext=O&term_code=201210&search_type=A&keyword=&kw_scope=all&kw_opt=all&subj_code=BIO&crse_numb=205&campus=*&instructor=*&instr_session=*&attr_type=*&mon=on&tue=on&wed=on&thu=on&fri=on&sat=on&sun=on&avail_flag=on" '/BANPROD/pkgyc_yccsweb.P_Results 
    Dim tempCookie As New CookieContainer 
    Dim encoding As New UTF8Encoding 
    Dim byteData As Byte() = encoding.GetBytes(postData) 

    System.Net.ServicePointManager.SecurityProtocol = Net.SecurityProtocolType.Ssl3 
    Try 

     tempCookie.GetCookies(New Uri("https://taylor.yc.edu/BANPROD/pkgyc_yccsweb.P_Results")) 
     'postData="web_session=" & tempCookie. 

     Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://taylor.yc.edu/BANPROD/pkgyc_yccsweb.P_Results"), HttpWebRequest) 
     postReq.Method = "POST" 
     postReq.KeepAlive = True 
     postReq.CookieContainer = tempCookie 
     postReq.ContentType = "application/x-www-form-urlencoded" 


     postReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; Media Center PC 4.0; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)" 
     postReq.ContentLength = byteData.Length 
     Dim postreqstream As Stream = postReq.GetRequestStream 
     postreqstream.Write(byteData, 0, byteData.Length) 
     postreqstream.Close() 
     Dim postresponse As HttpWebResponse 
     postresponse = DirectCast(postReq.GetResponse, HttpWebResponse) 
     tempCookie.Add(postresponse.Cookies) 

     Dim postresreader As New StreamReader(postresponse.GetResponseStream) 
     Dim thepage As String = postresreader.ReadToEnd 
     MsgBox(thepage) 
    Catch ex As WebException 
     MsgBox(ex.Status.ToString & vbNewLine & ex.Message.ToString) 
    End Try 

End Sub 

답변

2

문제는 tempCookie.GetCookies() 당신은 그것의 일을 생각하는 일을하지 않는 것입니다. 실제로 실제로 제공되는 것은 CookieCollection의 기본 URL을 필터링하여 제공된 URL의 쿠키 만 포함시킵니다. 대신, 먼저이 세션 토큰을 제공 할 페이지에 대한 요청을 작성한 다음 데이터에 대한 실제 요청을 작성하십시오. 먼저 P_Search에 페이지를 요청한 다음 CookieContainer이라는 요청을 다시 사용하고 P_Results에 게시하십시오.

그러나 HttpWebRequest 개체 대신 WebClient 클래스와 my post here about extending it to support cookies을 알려 드리겠습니다. 코드를 간소화 할 수 있다는 것을 알게 될 것입니다. 아래는 이것을 보여주는 VB2010 WinForms 앱입니다. 여전히 HttpWebRequest 개체를 사용하려면 최소한 수행해야 할 작업에 대한 아이디어를 제공해야합니다.

Option Strict On 
Option Explicit On 

Imports System.Net 

Public Class Form1 

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
     ''//Create our webclient 
     Using WC As New CookieAwareWebClient() 
      ''//Set SSLv3 
      System.Net.ServicePointManager.SecurityProtocol = Net.SecurityProtocolType.Ssl3 
      ''//Create a session, ignore what is returned 
      WC.DownloadString("https://taylor.yc.edu/BANPROD/pkgyc_yccsweb.P_Search") 
      ''//POST our actual data and get the results 
      Dim S = WC.UploadString("https://taylor.yc.edu/BANPROD/pkgyc_yccsweb.P_Results", "POST", "term_code=201130&search_type=K&keyword=math") 
      Trace.WriteLine(S) 
     End Using 
    End Sub 
End Class 

Public Class CookieAwareWebClient 
    Inherits WebClient 

    Private cc As New CookieContainer() 
    Private lastPage As String 

    Protected Overrides Function GetWebRequest(ByVal address As System.Uri) As System.Net.WebRequest 
     Dim R = MyBase.GetWebRequest(address) 
     If TypeOf R Is HttpWebRequest Then 
      With DirectCast(R, HttpWebRequest) 
       .CookieContainer = cc 
       If Not lastPage Is Nothing Then 
        .Referer = lastPage 
       End If 
      End With 
     End If 
     lastPage = address.ToString() 
     Return R 
    End Function 
End Class 
+0

위대했습니다. 나는 그것을 영원히 이해하려고 노력 했었고 그것을 올바르게 할 수 없었습니다. 당신의 도움을 주셔서 감사합니다! – Jon49

+0

동시 환경에서이 유형의 기능을 지원해야합니다. 나는 WebClient가 동시 I/O를 지원하지 않는다는 것을 알고 있지만 여러 웹 요청에 하나의 'CookieContainer'를 제공하여 단일 세션을 사용하는 방법이 있습니까? 필요한 경우 논리를 더 설명 할 수 있습니다. – Terry

+0

'synclock'을 사용할 수 있습니까? http://stackoverflow.com/a/396248/231316 –

관련 문제