socket下载

阿牛138588 2008-03-28 08:29:30
在使用socket进行http下载。比如下载http://www.abc.com/1.asp
请问该http头该怎么写?
假如需要使用get或者post提交值时,http头又该怎么写?比如提交 value1=test

有没有办法知道网页所包含的session及session里的值?

哪里有比较全面的文档或者参考资料?谢谢!
...全文
229 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
HarleyTung 2008-03-29
  • 打赏
  • 举报
回复
mark
changjiangzhibin 2008-03-29
  • 打赏
  • 举报
回复

FileStream myFile = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
Response.AddHeader("Accept-Ranges", "bytes");
//...
阿牛138588 2008-03-29
  • 打赏
  • 举报
回复
像我这个哪里出错了呢?
调用:
Dim a As New HtppClient
RichTextBox1.Text = a.GetHeml("http://www.baidu.com", "get", "")
类源码:
Imports System.Net
Imports System.Text.RegularExpressions
Imports System.Text
Imports System.Net.Sockets
Public Class HtppClient

Private Encode As System.Text.Encoding = System.Text.Encoding.Default
Public Function GetHeml(ByVal strUri As String, ByVal strMethod As String, ByVal strData As String) As String
strUri = strUri.ToLower
If strUri.StartsWith("http://") = False And strUri.StartsWith("https://") = False Then
strUri = "http://" & strUri
End If
Dim s As Socket = CreateScoket(strUri) '可以正确建立链接
If IsNothing(strUri) Then
Return ""
End If

Dim head As String = MadeHead(strMethod, strUri, strData)
s.Send(Encode.GetBytes(head))
Dim buffer(s.ReceiveBufferSize) As Byte
Dim a As New List(Of Byte())
While (s.Receive(buffer) > 0)
a.Add(buffer)
End While
s.Close()

Dim strR As String = "" ' Encode.GetString(bytD)
Return strR
End Function
Function CreateScoket(ByVal struri As String) As Socket
'获得目标网站主地址
Dim reg As New Regex("https?://([^/\?].+?\.){2}\w{2,3}(\.\w{2,3})?", RegexOptions.IgnoreCase)
Dim host As String = reg.Match(struri).Value
If host.Length = 0 Then Return Nothing
For Each ip As IPAddress In Dns.GetHostEntry(host).AddressList
Dim endPoint As New IPEndPoint(ip, 80)
Dim s As New Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
s.SendTimeout = 5000
s.Connect(endPoint)
If s.Connected Then Return s
Next

Return Nothing
End Function


Function MadeHead(ByVal strMethod As String, ByVal strUri As String, ByVal strData As String) As String
Dim sb As New StringBuilder
sb.Append(strMethod.ToUpper & " / HTTP/1.1" & vbCrLf)
sb.Append("Accept: */*" & vbCrLf)
sb.Append("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" & vbCrLf)
sb.Append("host: " & struri & vbCrLf)
If strMethod.ToUpper = "GET" Then
sb.Append("Connection: Close" & vbCrLf)
ElseIf strMethod.ToUpper = "POST" Then
If strData.Length = 0 Then
sb.Append("Content-Type: application/x-www-form-urlencoded" & vbCrLf)
sb.Append("Content-Length: " & Encode.GetByteCount(strData) & vbCrLf)
sb.Append("Connection: Keep-Alive" & vbCrLf & vbCrLf)
sb.Append(strData & vbCrLf)
End If
End If
Return sb.ToString
End Function


End Class
liqngjun123 2008-03-28
  • 打赏
  • 举报
回复
连接www.abc.com:80成功
(1) GET request:
GET /1.asp HTTP/1.1
Accept: */*
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Host: www.abc.com
Referer: http://www.abc.com
Pragma: no-cache
Cache-Control: no-cache
Connection: close

(2) POST:
POST / HTTP/1.1
Host: localhost:8181
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.0.7)
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 24

num=aa&kill=bb&United=cc
Cassava 2008-03-28
  • 打赏
  • 举报
回复
是读网页里的内容吧?应该不是socket下载而是http下载,但也只能取的经过IIS解释后产生的html内容,
是取不到session里的值的
sparks345 2008-03-28
  • 打赏
  • 举报
回复
~.~
duping9626 2008-03-28
  • 打赏
  • 举报
回复
mark

110,526

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧