日期:2014-05-18  浏览次数:20418 次

100分!如何将这两个asp函数改写为asp.net(vb.net)?
各位高手,如何将这两个asp函数改写为asp.net(vb语言)?先谢了!


Function   getHTTPPage(url)
        dim   objXML
        set   objXML=server.createobject( "MSXML2.XMLHTTP ") '定义
        objXML.open   "GET ",url,false '打开
        objXML.send() '发送
        If   objXML.readystate <> 4   then   '判断文档是否已经解析完,以做客户端接受返回消息
                exit   function
        End   If
        getHTTPPage=BytesToBstr(objXML.responseBody) '返回信息,同时用函数定义编码
        set   objXML=nothing '关闭
        if   err.number <> 0   then   err.Clear  
End   Function

Function   BytesToBstr(body)
dim   objstream
set   objstream   =   Server.CreateObject( "adodb.stream ")
        objstream.Type   =   1
        objstream.Mode   =3
        objstream.Open
        objstream.Write   body
        objstream.Position   =   0
        objstream.Type   =   2
        objstream.Charset   =   "GB2312 "    
        '转换原来默认的UTF-8编码转换成GB2312编码,否则直接用XMLHTTP调用有中文字符的网页得到的将是乱码
        BytesToBstr   =   objstream.ReadText  
objstream.Close
set   objstream   =   nothing
End   Function


------解决方案--------------------
不用改写...因为...有现成的可以直接用...

1.如果目标网页是格式良好的...非常简单...

Dim doc As System.Xml.XmlDocument
doc.Load(url)

如果不是就当作文本获取...

Dim request As System.Net.WebRequest
request = System.Net.WebRequest.Create(url)
Dim response As System.Net.WebResponse
response = request.GetResponse
Dim stream As System.IO.Stream
stream = response.GetResponseStream
Dim encode As Encoding = System.Text.Encoding.GetEncoding( "GB2312 ")
Dim sr As StreamReader = New StreamReader(stream, encode)
Console.WriteLine(sr.ReadToEnd)

2.调用 System.Text.Encoding 类...如上面的例子...
------解决方案--------------------
BytesToBstr》》
public object BytesToBstr(object body)
{
object instance = RuntimeHelpers.GetObjectValue(this.Server.CreateObject( "adodb.stream "));
NewLateBinding.LateSet(instance, null, "Type ", new object[] { 1 }, null, null);
NewLateBinding.LateSet(instance, null, "Mode ", new object[] { 3 }, null, null);
NewLateBinding.LateCall(instance, null, "Open ", new object[0], null, null, null, true);
object[] arguments = new object[] { RuntimeHelpers.GetObjectValue(body) };
bool[] copyBack = new bool[] { true };
NewLateBinding.LateCall(instance, null, "Write ", arguments, null, null, copyBack, true);
if (copyBack[0])
{
body = RuntimeHelpers.GetObjectValue(arguments[0]);
}
NewLateBinding.LateSet(instance, null, "Position ", new object[] { 0 }, null, null);
NewLateBinding.LateSet(instance, null, "Type ", new object[] { 2 }, null, null);
NewLateBinding.LateSet(instance, null, "Charse