Question HTTP POST method in .net

pisceswzh

Well-known member
Joined
Mar 19, 2007
Messages
96
Programming Experience
1-3
Dear all,

I am working on a project that need to ask my program to use the HTTP GET/POST method to interact with a web server. To be more specific, I need my program to log into a website with a set of user name and password from the database and then save the cookie returned from the website.

Anybody can give me any hint? I think I should use the System.Net.HttpRequest namespace, shouldn't I?

Any help or any reference would be appreciated. Thanks.

Tom
 
there is a code that can help u
VB.NET:
  Dim tmpURL As String = "http://somethin.com/?blogID=48613&postID=1572315&
blogName=sorkhekhorshid"
        Dim tmpPostingForm As String = "__EVENTTARGET=" & "&__EVENTARGUMENT=" & "
&__VIEWSTATE=/wEPDwULLTE4nMwg2K/kZTZEKIjTCLjN4mol7PQV3o5k5E=" &
 "&txtName=ngg" & "&txtEmail=" 
& "&txtWebsite=" & "&txtCommentBody=fghfgh" & 
"&btnSend=ارسال" & "&chksave=" & "&chkPrivate="
        Dim data As Byte() = System.Text.Encoding.ASCII.GetBytes(tmpPostingForm)
        Dim os As System.IO.Stream = Nothing
        Dim request As HttpWebRequest = Nothing
        request = WebRequest.Create(tmpURL)
        request.ContentType = "application/x-www-form-urlencoded"
        request.Method = "POST"
        request.AllowAutoRedirect = True
        request.AllowWriteStreamBuffering = True
        request.KeepAlive = True
        request.ContentType = "text/html; charset=utf-8"
        request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14"
        request.ContentLength = data.Length
        request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-us,en;q=0.5")
        request.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.7")
        request.Headers.Add(HttpRequestHeader.Pragma, "nocache")
        request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate")
        ' request.Timeout = 420000

        os = request.GetRequestStream()
        os.Write(data, 0, data.Length)
        os.Close()
        Dim resp As HttpWebResponse = Nothing
        resp = request.GetResponse
        Dim sr As System.IO.StreamReader = New System.IO.StreamReader(resp.GetResponseStream)
        Dim response As String = sr.ReadToEnd.ToString
        MsgBox(response)
 
Back
Top