Imports System.Net Imports System.IOPublic Class HttpDriverClass HttpDriver Public Function GetPage()Function GetPage(ByVal url As String, Optional ByRef postPara As String = "", Optional ByRef encType As String = "GB2312") As String Return GetPage(url, postPara, Nothing, False, encType) End Function Public Function GetPage()Function GetPage(ByVal url As String, ByRef postPara As System.Collections.Hashtable, Optional ByRef encType As String = "GB2312") As String Return GetPage(url, ColToStr(postPara), encType) End Function Public Function GetPage()Function GetPage(ByVal url As String, ByRef postPara As String, ByRef cookies As CookieCollection, ByVal hasCookie As Boolean, Optional ByRef encType As String = "GB2312", Optional ByRef refer As String = "") As String If (url.StartsWith("http://") = False) Then url = "http://" & url End If Dim hRqst As HttpWebRequest = HttpWebRequest.Create(url) If (hasCookie = True AndAlso (Not cookies Is Nothing)) Then hRqst.CookieContainer = New CookieContainer hRqst.CookieContainer.Add(cookies) End If hRqst.ContentType = "application/x-www-form-urlencoded" hRqst.Headers.Add("Accept-Language", "zh-cn") Dim streamData As Stream Dim bt() As Byte If (postPara = "") Then hRqst.Method = "GET" Else hRqst.Method = "POST" hRqst.AllowWriteStreamBuffering = True bt = System.Text.Encoding.ASCII.GetBytes(postPara) hRqst.ContentLength = bt.Length hRqst.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" hRqst.Referer = refer hRqst.KeepAlive = False hRqst.Timeout = 20000 streamData = hRqst.GetRequestStream() streamData.Write(bt, 0, bt.Length) streamData.Close() End If Dim hRsp As HttpWebResponse hRsp = hRqst.GetResponse() streamData = hRsp.GetResponseStream() If (hasCookie = True) Then cookies = hRsp.Cookies End If If (encType = "") Then encType = "GB2312" End If Dim readStream As New IO.StreamReader(streamData, System.Text.Encoding.GetEncoding(encType)) GetPage = readStream.ReadToEnd() streamData.Close() End Function Public Function GetPage()Function GetPage(ByVal url As String, ByRef postPara As System.Collections.Hashtable, ByRef cookies As CookieCollection, ByVal hasCookie As Boolean, Optional ByRef encType As String = "GB2312") As String Return GetPage(url, ColToStr(postPara), cookies, True, encType) End Function Public Function GetPage()Function GetPage(ByVal url As String, ByRef cookies As CookieCollection, ByVal hasCookie As Boolean, Optional ByRef encType As String = "GB2312") As String Return GetPage(url, "", cookies, True, encType) End Function '该函数用于转换表单项集合为字符串 Public Shared Function ColToStr()Function ColToStr(ByRef ht As System.Collections.Hashtable, Optional ByRef encType As String = "GB2312") As String Dim str As String Dim para As DictionaryEntry For Each para In ht str &= System.Web.HttpUtility.UrlEncode(CType(para.Key, String), Text.Encoding.GetEncoding(encType)) str &= "=" str &= System.Web.HttpUtility.UrlEncode(CType(para.Value, String), Text.Encoding.GetEncoding(encType)) str &= "&" Next str = str.Substring(0, str.Length - 1) Return str End FunctionEnd Class
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url)
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "post";
byte[] buffer = Encoding.Default.GetBytes("a=aaa&b=bbb");
request.ContentLength = buffer.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(buffer, 0, buffer.Length);
}
Imports System.IOPublic Class HttpDriverClass HttpDriver Public Function GetPage()Function GetPage(ByVal url As String, Optional ByRef postPara As String = "", Optional ByRef encType As String = "GB2312") As String
Return GetPage(url, postPara, Nothing, False, encType)
End Function Public Function GetPage()Function GetPage(ByVal url As String, ByRef postPara As System.Collections.Hashtable, Optional ByRef encType As String = "GB2312") As String
Return GetPage(url, ColToStr(postPara), encType)
End Function Public Function GetPage()Function GetPage(ByVal url As String, ByRef postPara As String, ByRef cookies As CookieCollection, ByVal hasCookie As Boolean, Optional ByRef encType As String = "GB2312", Optional ByRef refer As String = "") As String
If (url.StartsWith("http://") = False) Then
url = "http://" & url
End If
Dim hRqst As HttpWebRequest = HttpWebRequest.Create(url)
If (hasCookie = True AndAlso (Not cookies Is Nothing)) Then
hRqst.CookieContainer = New CookieContainer
hRqst.CookieContainer.Add(cookies)
End If hRqst.ContentType = "application/x-www-form-urlencoded"
hRqst.Headers.Add("Accept-Language", "zh-cn")
Dim streamData As Stream
Dim bt() As Byte
If (postPara = "") Then
hRqst.Method = "GET"
Else
hRqst.Method = "POST"
hRqst.AllowWriteStreamBuffering = True
bt = System.Text.Encoding.ASCII.GetBytes(postPara)
hRqst.ContentLength = bt.Length
hRqst.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
hRqst.Referer = refer
hRqst.KeepAlive = False
hRqst.Timeout = 20000
streamData = hRqst.GetRequestStream()
streamData.Write(bt, 0, bt.Length)
streamData.Close()
End If
Dim hRsp As HttpWebResponse
hRsp = hRqst.GetResponse()
streamData = hRsp.GetResponseStream()
If (hasCookie = True) Then
cookies = hRsp.Cookies
End If
If (encType = "") Then
encType = "GB2312"
End If
Dim readStream As New IO.StreamReader(streamData, System.Text.Encoding.GetEncoding(encType))
GetPage = readStream.ReadToEnd()
streamData.Close()
End Function Public Function GetPage()Function GetPage(ByVal url As String, ByRef postPara As System.Collections.Hashtable, ByRef cookies As CookieCollection, ByVal hasCookie As Boolean, Optional ByRef encType As String = "GB2312") As String
Return GetPage(url, ColToStr(postPara), cookies, True, encType)
End Function Public Function GetPage()Function GetPage(ByVal url As String, ByRef cookies As CookieCollection, ByVal hasCookie As Boolean, Optional ByRef encType As String = "GB2312") As String
Return GetPage(url, "", cookies, True, encType)
End Function '该函数用于转换表单项集合为字符串
Public Shared Function ColToStr()Function ColToStr(ByRef ht As System.Collections.Hashtable, Optional ByRef encType As String = "GB2312") As String
Dim str As String
Dim para As DictionaryEntry
For Each para In ht
str &= System.Web.HttpUtility.UrlEncode(CType(para.Key, String), Text.Encoding.GetEncoding(encType))
str &= "="
str &= System.Web.HttpUtility.UrlEncode(CType(para.Value, String), Text.Encoding.GetEncoding(encType))
str &= "&"
Next
str = str.Substring(0, str.Length - 1)
Return str
End FunctionEnd Class
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url)
request.Method = "post";
byte[] buffer = Encoding.Default.GetBytes("a=aaa&b=bbb");
request.ContentLength = buffer.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(buffer, 0, buffer.Length);
}
就是你要提交的数据啊
a是表单名字,aaa是值
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "post";
byte[] buffer = Encoding.Default.GetBytes("a=aaa&b=bbb");
request.ContentLength = buffer.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(buffer, 0, buffer.Length);
}
1. GET 方式。 GET 方式通过在网络地址附加参数来完成数据的提交,比如在地址 http://www.google.com/webhp?hl=zh-CN 中,前面部分 http://www.google.com/webhp 表示数据提交的网址,后面部分 hl=zh-CN 表示附加的参数,其中 hl 表示一个键(key), zh-CN 表示这个键对应的值(value)。程序代码如下: HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "http://www.google.com/webhp?hl=zh-CN" );
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
} 2. POST 方式。 POST 方式通过在页面内容中填写参数的方法来完成数据的提交,参数的格式和 GET 方式一样,是类似于 hl=zh-CN&newwindow=1 这样的结构。程序代码如下: string param = "hl=zh-CN&newwindow=1";
byte[] bs = Encoding.ASCII.GetBytes(param);HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "http://www.google.com/intl/zh-CN/" );
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
} 在上面的代码中,我们访问了 www.google.com 的网址,分别以 GET 和 POST 方式提交了数据,并接收了返回的页面内容。然而,如果提交的参数中含有中文,那么这样的处理是不够的,需要对其进行编码,让对方网站能够识别。 http://www.cnblogs.com/Aricc/archive/2008/05/23/1205410.html
string param = "author=yang&[email protected]&url=http://www.baidu.com&comment=sssss";
byte[] bs = Encoding.ASCII.GetBytes(param); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://192.168.1.249/wordpress/?p=21/");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length; using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}这样写,怎么提交不上去啊