HttpWebRequest POST和Cookie [英] HttpWebRequest POST and Cookies
本文介绍了HttpWebRequest POST和Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
code>函数GetPage(ByVal Url As String)As String
Dim CookieJar As New Net.CookieContainer
Dim enc As Encoding = Encoding.GetEncoding(1252)
Dim Data As Byte )= Nothing
Dim PostData As String =
如果InStr(Url,?)< 0 Then
PostData = Url.Substring(InStr(Url,?))
Url = Replace(Url,PostData,)
Url = Url.TrimEnd )
Data = enc.GetBytes(PostData)
结束如果
Dim req As System.Net.HttpWebRequest = CType(Net.WebRequest.Create(Url) ,Net.HttpWebRequest)
req.AllowAutoRedirect = False
req.ContentType =application / x-www-form-urlencoded
req.Method =POST
如果不是数据没有然后
如果Data.Length> 0 then
req.ContentLength = Data.Length
Dim newStream As Stream = req.GetRequestStream()
newStream.Write(Data,0,Data.Length)
newStream.Flush ()
newStream.Close()
如果
结束如果
$ b结束如果
req.CookieContainer = CookieJar
Dim Response As Net.HttpWebResponse = CType 。
Dim Html As String = ResponseStream.ReadToEnd()返回一个响应消息,返回一个响应消息,并返回一个响应消息。 )
Response.Close()
ResponseStream.Close()
返回Html
结束函数
我应该怎么办?
解决方案在向
$ c.GetRequestStream()
$ b写入任何数据之前设置.CookieContainer
查看以下示例:
CookieContainer cookies = new CookieContainer();
HttpWebRequest postRequest =(HttpWebRequest)WebRequest.Create(site);
postRequest.CookieContainer = cookies; // note this
postRequest.Method =POST;
postRequest.ContentType =application / x-www-form-urlencoded;
using(Stream stream = postRequest.GetRequestStream())
{
stream.Write(buffer,0,buffer.Length);
}
Hi am trying to make an application that post data to a joomla login page but the only thing i get back is cookies is not enabled.
Function GetPage(ByVal Url As String) As String Dim CookieJar As New Net.CookieContainer Dim enc As Encoding = Encoding.GetEncoding(1252) Dim Data As Byte() = Nothing Dim PostData As String = "" If InStr(Url, "?") <> 0 Then PostData = Url.Substring(InStr(Url, "?")) Url = Replace(Url, PostData, "") Url = Url.TrimEnd("?"c) Data = enc.GetBytes(PostData) End If Dim req As System.Net.HttpWebRequest = CType(Net.WebRequest.Create(Url), Net.HttpWebRequest) req.AllowAutoRedirect = False req.ContentType = "application/x-www-form-urlencoded" req.Method = "POST" If Not Data Is Nothing Then If Data.Length > 0 Then req.ContentLength = Data.Length Dim newStream As Stream = req.GetRequestStream() newStream.Write(Data, 0, Data.Length) newStream.Flush() newStream.Close() End If End If req.CookieContainer = CookieJar Dim Response As Net.HttpWebResponse = CType(req.GetResponse(), Net.HttpWebResponse) Dim ResponseStream As IO.StreamReader = New IO.StreamReader(Response.GetResponseStream(), enc) Dim Html As String = ResponseStream.ReadToEnd() Response.Close() ResponseStream.Close() Return Html End Function
How should i do?
解决方案Try to set
.CookieContainer
before writing any data to.GetRequestStream()
Look this sample:
CookieContainer cookies = new CookieContainer(); HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create(site); postRequest.CookieContainer = cookies; // note this postRequest.Method = "POST"; postRequest.ContentType = "application/x-www-form-urlencoded"; using (Stream stream = postRequest.GetRequestStream()) { stream.Write(buffer, 0, buffer.Length); }
这篇关于HttpWebRequest POST和Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文