WebRequest保持Session和Cookie

最近用到了webrequest,查了很多资料都无法保存网站的登录信息

最后自己终于调试成功了一把。

Option   Explicit   On  
Option   Strict   On

Imports  System.Net
Imports  System.Web
Imports  System.Text
Imports  System.IO

Namespace SoilNET.KRL.WebUtility

    
Public Class LoginRequest
        
Public Cookies As CookieCollection


        
Sub New()

        
End Sub

        
Public Function Login(ByVal Url As StringByVal Params As StringAs String
            
Dim strResult As String = getPageByPost(Url, Params)
            
Return strResult
        
End Function


        
Public Function getPageByPost(ByVal url As StringByVal payload As StringOptional ByVal needEncode As Boolean = FalseAs String
            
Dim result As HttpWebResponse
            
Dim strResult As String = 
            
Try
                
Dim req As HttpWebRequest
                
Dim RequestStream As Stream
                
Dim ReceiveStream As Stream
                
Dim encode As Encoding
                
Dim sr As StreamReader

                req 
= CType(WebRequest.Create(url), HttpWebRequest)
                req.Method 
= POST
                req.ContentType 
= applicationx-www-form-urlencoded
                req.CookieContainer 
= New CookieContainer
                req.UserAgent 
= Mozilla4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461; .NET CLR 1.0.3705)

                
If Not Me.Cookies Is Nothing Then
                    req.CookieContainer.Add(
New Uri(url), Me.Cookies)
                
End If


                
Dim SomeBytes() As Byte
                
Dim UrlEncoded As New StringBuilder
                
Dim reserved() As Char = {ChrW(63), ChrW(61), ChrW(38)}

                
If payload  Nothing Then
                    
If needEncode Then '有些网站encode后反而出现中文乱码所以加上这个开关
                        Dim i As Integer = 0
                        
Dim j As Integer
                        
While i  payload.Length
                            j 
= payload.IndexOfAny(reserved, i)
                            
If j = -1 Then
                                UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, payload.Length 
- i)))
                                
Exit While
                            
End If
                            UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, j 
- i)))
                            UrlEncoded.Append(payload.Substring(j, 
1))
                            i 
= j + 1
                        
End While
                        SomeBytes 
= System.Text.Encoding.ASCII.Default.GetBytes(UrlEncoded.ToString())
                    
Else
                        SomeBytes 
= System.Text.Encoding.ASCII.Default.GetBytes(payload)
                    
End If
                    req.ContentLength 
= SomeBytes.Length
                    RequestStream 
= req.GetRequestStream()
                    RequestStream.
Write(SomeBytes, 0, SomeBytes.Length)
                    RequestStream.Close()
                
Else
                    req.ContentLength 
= 0
                
End If
                
'result.Cookies = New CookieCollection
                result = CType(req.GetResponse(), HttpWebResponse)


                ReceiveStream 
= result.GetResponseStream()
                encode 
= System.Text.Encoding.ASCII.Default
                sr 
= New StreamReader(ReceiveStream, encode)

                
Dim read(256As Char
                
Dim count As Integer = sr.Read(read, 0256)

                
Do While count  0
                    strResult 
&= New String(read, 0, count)
                    count 
= sr.Read(read, 0256)
                
Loop
                Me.Cookies 
= req.CookieContainer.GetCookies(New Uri(url))

            
Catch Exc As Exception

                Debug.
Write(Exc.Message)
            
Finally

                
If Not result Is Nothing Then
                    result.Close()
                
End If

            
End Try
            
Return strResult
        
End Function

        
Public Function getPageByUrl(ByVal url As StringAs String
            
Dim result As HttpWebResponse
            
Dim strResult As String = 
            
Try
                
Dim req As HttpWebRequest

                
Dim ReceiveStream As Stream
                
Dim encode As Encoding
                
Dim sr As StreamReader

                req 
= CType(WebRequest.Create(url), HttpWebRequest)

                req.Method 
= GET

                req.CookieContainer 
= New CookieContainer
                req.UserAgent 
= Mozilla4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461; .NET CLR 1.0.3705)

                
If Not Me.Cookies Is Nothing Then
                    req.CookieContainer.Add(
New Uri(url), Me.Cookies)
                
End If

                result 
= CType(req.GetResponse(), HttpWebResponse)


                ReceiveStream 
= result.GetResponseStream()
                encode 
= System.Text.Encoding.ASCII.Default
                sr 
= New StreamReader(ReceiveStream, encode)

                
Dim read(256As Char
                
Dim count As Integer = sr.Read(read, 0256)

                
Do While count  0
                    strResult 
&= New String(read, 0, count)
                    count 
= sr.Read(read, 0256)
                
Loop
                Me.Cookies 
= req.CookieContainer.GetCookies(New Uri(url))
            
Catch Exc As Exception


            
Finally

                
If Not result Is Nothing Then
                    result.Close()
                
End If

            
End Try
            
Return strResult
        
End Function

    
End Class

End Namespace

你可能感兴趣的:(session)