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 |
|
相关文章推荐
- VB.net 2005 向服务器POST信息(上传文件等)
- HTTP协议中GET、POST和HEAD的介绍 2008-05-10 14:15 GET: 请求指定的页面信息,并返回实体主体。 HEAD: 只请求页面的首部。 POST: 请求服务器接受所指定的文
- Java爬虫(一)利用GET和POST发送请求,获取服务器返回信息
- Java爬虫(一)利用GET和POST发送请求,获取服务器返回信息
- vb.net DirectoryNotFoundException UnauthorizedAccessException FileSystem.GetFiles获取文件 异常 权限解决
- asp.net 使用ajax请求地址接口数据 ajax返回类型是json js get post请求数据
- vb.net上传文件到FTP服务器
- C# 模拟post数据提交时 出现如下错误: System.Net.WebException: 远程服务器返回错误: (417) Expectation Failed 的解决办法
- .net编写客户端实现正方教务网登录,查询课表,查询考试等信息程序(Post,Get等)---潘尚
- VB.Net 获取文件详细信息(版本、公司等)
- VB.NET FTP服务器上下载文件
- C# post数据时 出现如下错误: System.Net.WebException: 远程服务器返回错误: (417) Expectation Failed 的解决办法
- VB.Net程序中对.Config文件读写的类
- asp.net C#获取程序文件相关信息
- 在.Net中嵌入资源文件到程序集中-.NET教程,VB.Net语言
- SEO_ASP.net SEO优化(包含URL地址重写\viewState移动和压缩至服务器\SEO信息XML生成_根据URL地址重写文件)\web网站内容压缩 源码公开.本人授权可使用于商业项目。
- asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- Android将程序崩溃信息保存本地文件发送至服务器
- VB对文件进行读取信息并返回到列表框条目
- 【Android】Android与服务器交互 POST上传多个图片文件、文本内容 GET下载图片