您的位置:首页 > 编程语言 > VB

VB.NET-用Socket类写个POST/GET程序.在服务器返回中的信息里,中文件为何变成了编码??

2005-08-28 03:34 525 查看
我用VB.NET写了个Socket程序.作用是用于GET或POST网站.
当我用该程发出POST命令时,服务器返回信息中的中文却变成了其它编码!
怎么样才能将服务返回信息中的汉字不要变成其它编码?
如我发POST信息为(下面其中两处的"11111"为用户名和密码,是不存在的用户名):
POST /getin.cgi HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://www.***.com/getin.cgi Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)
Host: www.****.com
Content-Length: 31
Connection: Keep-Alive
Cache-Control: no-cache

id=&username=11111&passwd=11111
=======================================================================
''''***************把这个发给服务器后,将返回以下信息:**************
=======================================================================
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Sat, 20 Aug 2005 07:41:51 GMT
Connection: close
Content-Type: text/html

<center><table border=0 height=60% align=center><tr><td height=60% valign=middle align=center><font size=3 color=red>6T2;Fp#,4KSC;';9C;W"2a!#</font><p><a href=javascript:history.back()><font color=000000 size=2>75;X</font></a></td></tr></table>

在这些返回的信息中,其中(6T2;Fp#,4KSC;';9C;W"2a!#)原本是汉字的(对不起,此用户还没注册。)意思(我同样用VB写了个POST程序,返回的信息的汉字是正常)
所以不是为什么我用VB.NET的Socket类写同样的效果程序返回的信息中带有的汉字却变成了类似上面的编码(也不知是什么编,是不是UNICODE编?).有什么办法可以让它显示汉字吗?
以下为Socket类片段代码(主要的那部份):
Private Sub startmain()'''开始运行........
Dim cilen1 As New cilen(hostip, hostport)''''hostip是服务IP或地址,hostport是服务器端口
Dim str As String
str = TextBox4.Text''''TextBox4.text中的内容为上面POST命令要发送的信息
cilen1.abc(str)
cilen1 = Nothing
End Sub

Class cilen
Public str As String ''''''返回网站的代号
Public sip As String
Public sport As String
Dim surl As String
Public Bool As Boolean
Dim serverIP As IPAddress
Dim serverhost As IPEndPoint
Dim clientSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

Dim httpReq As String
Dim buffer(1024) As Byte
Dim bufferhttp(1024) As Byte
Dim byteCount As Int16
Public Sub New(ByVal ip As String, ByVal port As String)
On Error GoTo ee
sip = ip
sport = port

serverIP = Dns.Resolve(sip).AddressList(0)

serverhost = New IPEndPoint(serverIP, 80)
Exit Sub
ee:

End Sub ''''构造函数
Public Function rconnect()

If serverIP Is Nothing Then
MsgBox("无法连接此网站!请点击停止", 0, "提示")
Exit Function
End If
Try
clientSocket.Connect(serverhost)
If clientSocket.Connected = False Then
MsgBox("1")
MsgBox("Connect Error.", MsgBoxStyle.Critical, "HTTP")
Exit Function
End If
Catch ex As Exception
rconnect = "1"
MsgBox("2")
MsgBox(ex.StackTrace.ToString(), MsgBoxStyle.Critical, "Exception")
End Try

End Function '''重新连接
Public Sub abc(ByVal url As String)
If serverIP Is Nothing Then
MsgBox("无法连接此网站!请点击停止进行结束!", 0, "提示")
GoTo ee
End If

str = rconnect()
If str = "1" Then
GoTo ee
End If
surl = url
Try
httpReq = surl & ControlChars.CrLf & ControlChars.CrLf
bufferhttp = ASCII.GetBytes(httpReq)
clientSocket.Send(bufferhttp, Len(httpReq), SocketFlags.None)
byteCount = clientSocket.Receive(buffer, buffer.Length, 0)
str = ASCII.GetString(buffer, 0, byteCount) + ControlChars.CrLf
Do While byteCount > 0
byteCount = clientSocket.Receive(buffer, buffer.Length, 0)
str = str & ASCII.GetString(buffer, 0, byteCount) + ControlChars.CrLf
Loop

Catch ex As Exception
MsgBox(ex.StackTrace.ToString(), MsgBoxStyle.Critical, "Exception")
GoTo ee
End Try
clientSocket.Shutdown(SocketShutdown.Both)
clientSocket.Close()
serverhost = Nothing
Exit Sub
ee:
clientSocket.Close()
End Sub ''''发送命令并获取数据

Protected Overrides Sub finalize()
'MsgBox("完成任务!", 0, "提示")
End Sub ''''析构函数
End Class

其中在类Class cilen中的变量字符串str就是服务返回的信息,怎么样把str里面的信息中原本是汉字的编码变成原样?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐