2012-03-26 134 views
1

我試圖在VB.Net中使用POST請求登錄到cPanel。登錄時和發佈時我有正確的憑據,當它應該是'301'(使用Tamper Data Firefox Add-On進行分析)時,仍然會收到'未經授權(401)'響應。以下是我的發帖要求信息和功能。VB.Net 401未經授權的HTTP Web請求

Private Function POSTreq(ByVal URL$, ByVal Data$) 
     Dim tempCookie As New CookieContainer 
     Dim DataBytes As Byte() = Encoding.ASCII.GetBytes(Data) 
     Dim Request As HttpWebRequest = TryCast(WebRequest.Create(URL), HttpWebRequest) 
     Request.Method = "POST" 
     Request.ContentType = "application/x-www-form-urlencoded" 
     Request.ContentLength = DataBytes.Length 

     Dim PostData As Stream = Request.GetRequestStream() 
     PostData.Write(DataBytes, 0, DataBytes.Length) 
     PostData.Close() 

     Dim Response As HttpWebResponse = Request.GetResponse() 
     Dim ResponseStream As Stream = Response.GetResponseStream() 
     Dim StreamReader As New StreamReader(ResponseStream) 
     Dim Text$ = StreamReader.ReadToEnd() 

     Return Text 
    End Function 

發佈網址

http://example.com:2082/login/ 

後數據

login_theme=cpanel&user=USERNAME&pass=PASSWORD&goto_uri=%2F 

回答

2

我可以複製你的代碼你描述的行爲。

如果我把它工作在我身邊的CookieContainer,我是能夠登錄:

rem ... 
Request.CookieContainer = tempCookie 
Request.Method = "POST" 
rem ... and so on ... 

第二種解決方案是隻提供憑據:

rem ... 
Dim myFullUri = new Uri(URL) 
Dim myCredentials As New NetworkCredential(Username, Password) 
Dim myCache As New CredentialCache() 

rem Add the credentials for that specific host and 
rem for "Basic" authentication only 
myCache.Add(New Uri(myFullUri.Scheme & "://" & myFullUri.Authority), _ 
    "Basic", myCredentials) 

Request.Credentials = myCache 

Request.CookieContainer = tempCookie 
Request.Method = "POST" 
rem ... and so on ... 
相關問題