以下是引用olivetchan在2012-7-19 10:48:00的发言:
开发指南中我发现使用了xmlhttp,依赖它(msxml.dll)还不如依赖.NET的WebRequest
以下内容为程序代码:
1 '''HttpPost
2 Dim myReq As Net.WebRequest
3 Dim myUri As New Uri(args(0))
4 myReq =Net.WebRequest.Create(myUri)
5 myReq.C
6 myReq.Method = "POST"
7 Dim bytes() As Byte
8 bytes = Encoding.UTF8.GetBytes(args(1))
9 Dim os As IO.Stream
10 os=Nothing
11 try
12 myReq.ContentLength = bytes.Length
13 os = myReq.GetRequestStream()
14 os.Write(bytes, 0, bytes.Length)
15 catch
16 Return "ERR_POST_REQ"
17 finally
18 If not os Is Nothing Then
19 os.Close()
20 End If
21 End try
22 try
23 Dim myResp As NET.WebResponse = myReq.GetResponse()
24 If myResp Is Nothing Then
25 Return "Err"
26 Else
27 Dim sr As IO.Stream = myResp.GetResponseStream()
28 Dim rs As New IO.StreamReader(sr, System.Text.Encoding.GetEncoding("utf-8"))
29 Dim read(256) As [Char]
30 Dim count As Integer = rs.Read(read, 0, 256)
31 Dim s As String
32 While count > 0
33 Dim str As New [String](read, 0, count)
34 s=s+str
35 count = rs.Read(read, 0, 256)
36 End While
37 Return s
38 End If
39 catch
40 Return "ERR_POST_RES"
41 End try
42
43
44 '''HttpGet
45 Dim myReq As Net.WebRequest
46 Dim myUri As New Uri(args(0))
47 myReq =Net.WebRequest.Create(myUri)
48 myReq.Timeout = args(2)
49 try
50 Dim webResponse As NET.HttpWebResponse= myReq.GetResponse()
51 If webResponse Is Nothing Then
52 Return "Err"
53 Else
54 Dim Charset As String ="utf-8"
55 If args(1)<>"" Then
56 Charset=args(1)
57 End If
58 Dim sr As IO.Stream = webResponse.GetResponseStream()
59 Dim rs As New IO.StreamReader(sr, System.Text.Encoding.GetEncoding(Charset))
60 Dim read(256) As [Char]
61 Dim count As Integer = rs.Read(read, 0, 256)
62 Dim s As String
63 While count > 0
64 Dim str As New [String](read, 0, count)
65 s=s+str
66 count = rs.Read(read, 0, 256)
67 End While
68 Return s
69 End If
70 catch
71 Return "ERR_POST_RES"
72 End try
73
这个代码拷到命令窗口不执行,如何使用?