问题如何Request 接收发来的二进制流

kenlistian 2008-03-04 05:55:51
在asp中,我们常用request接收客户端数据,
当我在request处理post来的xml字符串时没有问题,但是,当对方采用java,以字节流方式发送过来,我这里却不能正常解析该xml串。
请问如何处理,我采用如下的方法也不能正确解析,请高手指点。。。


Function getString(StringBin)
getString =""
For intCount = 1 to LenB(StringBin)
getString = getString & chr(AscB(MidB(StringBin,intCount,1)))
Next
End Function

Set xml = Server.CreateObject ("msxml2.DOMDocument")
xml.Async = False
iReceive = Request.TotalBytes

vtBinaryContent = Request.BinaryRead(iReceive)
strContent = getString(vtBnaryContent)
xml.Load strContent

if xml.parseError.errorCode <> 0 Then
sLog = "don't receive data:" & "Description: " & xml.parseError.reason & "<br>Line: " & xml.parseError.Line
Response。Write sLog
Response.End()
End If
...全文
1063 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
kenlistian 2008-03-05
  • 打赏
  • 举报
回复
等待中,
我对以上接收xml代码做了一个测试,发现返回的是BinaryRead没有正常读出,

iReceive = Request.TotalBytes
set vtBinaryContent = Request.BinaryRead(iReceive)

'测试是否返回对应的类型,返回竟然是vtEmpty ,即0,难道读不出?
Response.Write vartype(vtBinaryContent)

strContent = getString(vtBnaryContent)
kenlistian 2008-03-05
  • 打赏
  • 举报
回复
问题得以解决。
首先,对自己犯了几个低级错误做认识。没办法,只有在调试中学习了。
1.对Set vtBinaryContent = Request.BinaryRead(iReceive) 一句是不能用set,
因为set只是对类实例化有效,否则这在做vartype得到vtempty
这样

Dim vtBinaryContent
iReceive = Request.TotalBytes
Response.Write iReceive
’去掉set既可。
vtBinaryContent = Request.BinaryRead(iReceive)
strContent = getString(vtBnaryContent)
2.得意于
http://www.west263.com/www/info/50042-1.htm启示,
改getString 函数,是作为'把字节流转换为字符串,
为如下:
function getString(vt)
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
getString = strReturn
ent if
我所列的以前转换函数,其中对字节流转换字符流都是成功的,

3.得到该字符串后,其为utf-8编码,如果写到网页上,则可以正常解析,但是如果你要是直接加到
xml.loadxml 解析控件中,则无法正常解析出来,因为其中的编码是utf-8格式,就是那些%23%ED之类的,
则要把该url解析还原成,这个只需要在asp找个urlDeCode解码部分,把那些%去掉,在通过xml.loadxml即可处理成功。

具体分析见俺的博客。
http://www.cppblog.com/kenlistian/archive/2008/03/05/43751.html.








kenlistian 2008-03-05
  • 打赏
  • 举报
回复
用c#写当然可以啊,但是发送方是别人的,已经用java写好了,我在接收端采用asp写接收处理部分,问题就是
asp用Request好像无法接收到java采用字节流方式的post,(应该还是asp处理问题)
asp中的Request就只有一个BinaryRead,
当我采用
Request.BinaryRead(totalBytes)却好像读不出来数据,为啥我怀疑没有接收到数据,
我的局部测试代码是这样:
Dim vtBinaryContent

iReceive = Request.TotalBytes
'已传来数据
Response.Write iReceive
’读出字节流
Set vtBinaryContent = Request.BinaryRead(iReceive)
'由于BinaryRead能读出数据则vtBinaryContent应该是一个数组数据类型,即下列显示12
'结果显示的是0,就是vbEmpty,也就是没有初始化的变量,也就意味着binaryWrite根本没有读出接收的数据,
Response.Write "<br>receive type:" & varType(vtbinaryContent)

strContent = getString(vtBnaryContent)
xml.Load strContent





mantti 2008-03-05
  • 打赏
  • 举报
回复

<%@ Page Language="C#" %>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
string filePath = System.Configuration.ConfigurationSettings.AppSettings["UpLoadPath"] + "/";
string fileName;

//接受保存上传文件
foreach(string f in Request.Files.AllKeys)
{
string fullName;
HttpPostedFile postFile = Request.Files[f];
if (postFile.ContentLength > 10)
{
fileName = postFile.FileName;
GetFileName(fileName, out fullName);
fullName = filePath + fullName;
postFile.SaveAs(Server.MapPath(fullName));
Response.Write(fullName.Trim()); //客户端的返回值。
}
}

}


//根据时间和上传文件名生成新的文件名
void GetFileName(string fileName, out string fullName)
{
int lastDot = fileName.LastIndexOf(".");
string fileExtension = fileName.Substring(lastDot, fileName.Length-lastDot);
DateTime current = DateTime.Now;
int year, month, day, hour, minute, second, millsecond;
year = current.Year;
month = current.Month;
day = current.Day;
hour = current.Hour;
minute = current.Millisecond;
second = current.Second;
millsecond = current.Millisecond;
fullName = year.ToString() + month.ToString() + day.ToString() + hour.ToString() + minute.ToString() + second.ToString() + millsecond.ToString() +

fileExtension;
}
</script>
kenlistian 2008-03-04
  • 打赏
  • 举报
回复
其中发送的java部分处理如下:
public static void main(String[] args) throws Exception
{
HttpSend sender = new HttpSend(10 * 1000);
String url = "http://127.0.0.1/myxml/remo.asp";
String xml = "<?xml version = \"1.0\" encoding=\"UTF-8\" ?><ROOT><MM><ID>291AC5FDBD0DF4EF210<ID>23412</ID><NAME>zcas1</NAME><SEX>boy</SEX></MM></ROOT>";

sender.sendPost(url, URLEncoder.encode(xml, "UTF-8"));
..
}
其中 sendPost 是以把HttpClient类包起来做的一个类中的函数,这个其中xml字符串,编码按utf-8post到asp处理,
。。。。
在sendPost内部,其实就是调用这个函数new StringRequestEntity(message)设置post体,

而StringRequestEntity是httpclient类中的辅助类,处理如下:
# public StringRequestEntity(String content) {
# super();
# if (content == null) {
# throw new IllegalArgumentException("The content cannot be null");
# }
# this.contentType = null;
# this.charset = null;
# this.content = content.getBytes();
# }
可以看到,一个utf-8的字符串,是按getBytes() 方式,即字节方式发送,

我在以asp做测试程序时,以字符串发送是没有问题的。
如:
xml124="<?xml version = ""1.0"" <ROOT>.........</ROOT>"
Dim Https
Set Https = server.createobject("MSXML2.XMLHTTP")
Https.open "POST", "http://127.0.0.1/reMO.asp",False
Https.send xml124
...

reMo.asp中直接以Request接收即可。
而对方以字节方式发送,在直接用Request接收的话,估计不行,而必须采用binaryRead方式,
但是读出后如何把这个字节串还原成一个utf-8的字符串,则不知道上面程序错在哪里?













什么都不能 2008-03-04
  • 打赏
  • 举报
回复
把发送的代码贴出来
  • 打赏
  • 举报
回复
这个很多例子了
你看那些天天在问把图片以二进制存进数据库里的做法就明白了
kenlistian 2008-03-04
  • 打赏
  • 举报
回复
以下2种方法我都采用,好像不行,问题是我这样做方法正确吗?


'采用 ado record来转换
function rsbinarytostring(xbinary)
Dim binary
'multibyte data must be converted to vt_ui1 | vt_array first.
if vartype(xbinary)= 8 then
binary = multibytetobinary(xbinary)
else
binary = xbinary

dim rs, lbinary
const adlongvarchar = 201
set rs = createobject("adodb.recordset")
lbinary = lenb(binary)

if lbinary>0 then
rs.fields.append "mbinary", adlongvarchar, lbinary
rs.open
rs.addnew
rs("mbinary").appendchunk binary
rs.update
rsbinarytostring = rs("mbinary")
else
rsbinarytostring = ""
end if
end function

'采用流方式转换
function stream_binarytostring(binary, charset)
const adtypetext = 2
const adtypebinary = 1
dim binarystream as new stream
set binarystream = createobject("adodb.stream")

binarystream.type = adtypebinary
binarystream.open
binarystream.write binary
binarystream.position = 0
binarystream.type = adtypetext
if len(charset) > 0 then
binarystream.charset = charset
else
binarystream.charset = "us-ascii"
end if
stream_binarytostring = binarystream.readtext
end function
hookee 2008-03-04
  • 打赏
  • 举报
回复
可利用ADODB.Stream组件将Binary转为Text

28,409

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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