1

您好我正在尝试制作一个将数据发布到 joomla 登录页面的应用程序,但我唯一得到的是未启用 cookie。

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

我应该怎么做?

4

1 回答 1

6

.CookieContainer在写入任何数据之前尝试设置.GetRequestStream()

看这个样本:

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);
}
于 2009-10-17T11:15:44.893 回答