视频下载 网友评价 课程大纲 报名咨询

Web技术之Http协议

[ 890 查看 / 3 回复 ]

最近在老师这里上了WebService和Ajax,在课程中少不了要研究几个Header(Http Header & SoapHeader),在工作中正好用上。其中,关于 Http 协议的部分, 由于其使用面非常广,因此引发了很强烈的思考。

先看下面这段代码。
  1. public void write(String message, List<? extends Object> recipients) {
  2.                 int length = message.getBytes().length + 1;
  3.                 String httpHeader = "POST /sns HTTP/1.1\nContent-Length:" + length
  4.                                 + "\n\n";
  5.                 logger.info(httpHeader + message);
  6.                 session.write(httpHeader + message);
  7.         }
复制代码
用这个write方法来组装拼凑一个Http协议的全部。
其中,1。"POST /sns HTTP/1.1"是 状态行;(明文抄打,就是这一串字符串)
2。"Content-Length:" + length (典型的‘谁变化封装谁’,length是被封装的部分,运行时二次编译之后应该是这样的形式:"Content-Length:  276"——这个数字是昨天加班到10点,看到领导们一遍又一遍测试的,所以印象深刻 ),这是著名的 Http Header。
3。message部分是 Http协议的 ‘Http Body’,如果刚巧这个部分正好是一个xml,而且这个xml又刚巧是如下这般的形式:
  1. <env:Envelope>
  2. <env:Body>
  3. 中间是可以传递的数据,暂时忽略
  4. </env:Body>
  5. </env:Envelope>
复制代码
,则这个消息体就会被加上一个别名:Soap。当然,照猜想和推断,老师在WebService课程中巧妙运用的SoapHeader(查了相关资料,这是.net独用,java并没有对应实现)则是在上述Http Body中的Soap中再加入<env:Header></env:Header>。

上面的论述不够严谨,在W3CSchool上面查询得到格式良好的Soap应该如下:
  1. <?xml version="1.0"?>
  2. <soap:Envelope
  3. xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
  4. soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  5.   ...
  6.   Message information goes here
  7.   ...
  8. </soap:Envelope>
复制代码
这样,在标准的Http与XML绑定(双剑合璧==SOAP)之后,形成的用于网络传输的格式,应该像下面这个样子(这次学乖,直接copy&paste)
  1. POST /InStock HTTP/1.1
  2. Host: [url]www.example.org[/url]
  3. Content-Type: application/soap+xml; charset=utf-8
  4. Content-Length: nnn

  5. <?xml version="1.0"?>
  6. <soap:Envelope
  7. xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
  8. soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

  9. <soap:Body xmlns:m="http://www.example.org/stock">
  10.   <m:GetStockPrice>
  11.     <m:StockName>IBM</m:StockName>
  12.   </m:GetStockPrice>
  13. </soap:Body>

  14. </soap:Envelope>
复制代码
1

评分次数

    本主题由 管理员 hhjjj444 于 2010-1-16 19:36:18 执行 主题置顶/取消 操作
    TOP

    1. <?xml version="1.0" encoding="UTF-8"?>
      <data>
      <ratelog>
          <rateid>43</rateid>
          <uid>9</uid>
          <username>张波老师</username>
          <extcredits>1</extcredits>
          <extcreditsname>威望</extcreditsname>
          <extcreditsunit></extcreditsunit>
          <postdatetime>2010-01-15 13:03:03</postdatetime>
          <score>+30</score>
          <reason>精品文章</reason>
      </ratelog>
      </data>
    复制代码
    本页面加载的时候,firebug自动提示有一个小的ajax。。但是具体是 哪部分 的,尚不明了,暂时记录一下!
    TOP

    显示评分记录的.
    TOP

    学习一下
    TOP