`
wayfarer
  • 浏览: 296645 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

HTTP协议

阅读更多

1. 定义 (摘自midp2.0 doc)

HTTP is a request-response protocol in which the parameters of request must be set before the request is sent . The connection exists in one of three states:

  • Setup, in which the request parameters can be set
  • Connected, in which request parameters have been sent and the response is expected
  • Closed, the final state, in which the HTTP connection as been terminated

The following methods may be invoked only in the Setup state:

  • setRequestMethod
  • setRequestProperty

The transition from Setup to Connected is caused by any method that requires data to be sent to or received from the server.

The following methods cause the transition to the Connected state when the connection is in Setup state.

  • openInputStream
  • openDataInputStream
  • getLength
  • getType
  • getEncoding
  • getHeaderField
  • getResponseCode
  • getResponseMessage
  • getHeaderFieldInt
  • getHeaderFieldDate
  • getExpiration
  • getDate
  • getLastModified
  • getHeaderFieldKey

The following methods may be invoked while the connection is in Setup or Connected state.

  • close
  • getRequestMethod
  • getRequestProperty
  • getURL
  • getProtocol
  • getHost
  • getFile
  • getRef
  • getPort
  • getQuery

After an output stream has been opened by the openOutputStream or openDataOutputStream methods, attempts to change the request parameters via setRequestMethod or the setRequestProperty are ignored. Once the request parameters have been sent, these methods will throw an IOException . When an output stream is closed via the OutputStream.close or DataOutputStream.close methods , the connection enters the Connected state. When the output stream is flushed via the OutputStream.flush or DataOutputStream.flush methods, the request parameters MUST be sent along with any data written to the stream.

The transition to Closed state from any other state is caused by the close method and the closing all of the streams that were opened from the connection.

 

2. HTTP协议特点

(1) Http协议限制每次连接只处理一个请求。服务器处理完客户的请求并收到客户的应答后,即可断开连接 。由于Http是基于TCP/IP协议的,事实上它是面向连接的;

(2) Http协议是无状态协议。无状态是指协议对于事务处理没有记忆能力。缺少状态意味着如果后续处理需要前面的信息,则它必须重传 ,这样可能导致每次连接传送的数据量增大。Http协议的无状态特性为我们开发IM之类的软件增加了困难,客户端必须不停地轮循服务器以获得最新数据。

(3) Http支持Client-Server模式 ,每一个信息交换分为四 步:建立连接、发送请求信息、发送响应信息、关闭连接,这四步的划分是由于Http协议是基于请求/响应模式的

(4) Http允许传输任意类型的数据对象。正在传输的类型由Content-Type加以标记 ,eg:txt文件的 Content-Type = "text/plain"; 当Client向Server请求服务时,Http协议只需传送请求方法(GET/POST/HEAD)和路径即可。

 

3. MIDP规范支持HTTP协议的子集 ,规范中没有要求必须使用基于IP的网络来实现HTTP协议支持,如TCP/IP。MIDP允许HTTP实现使用非IP协议,如WAP,非IP协议则需要一个网关作为代理来访问Internet上的服务器。


4. 网络地址转换

    当移动电话访问位于公网上的服务器时,运营商会为移动电话分配一个IP地址。一般来说,他们分配的地址是动态IP地址(用动态主机配置协议Dynamic Host Configuration Protocol, DHCP分配)。但这些地址无法从公网上直接访问,因此需要一台网络地址转换(Network Address Translation, NAT)网关来执行电话IP地址与公共IP地址之间的映射。

    每当电话发起对互联网的UDP或TCP连接时,NAT网关都会创建一个映射,把该电话的IP地址和端口号映射到网关自己的IP地址和一个新端口号。互联网主机会把这个IP地址和端口号看做是TCP连接或UDP数据报的原始地址和端口,并把答复信息发送到这个原始地址和端口。然后,该NAT网关再将其映射回电话的IP地址和端口号,并将答复信息转发给电话。因此,从Web服务器日志上看到的IP地址并非手机的IP地址,而是网关的地址。

    运营商的NAT网关也会不断跟踪与该映射相关的服务器,并拒绝来自其他服务器指向该映射的“响应”。这是一种有用的防火墙功能,因为NAT网关的IP地址是众所周知的,其端口号也很容易被猜到,如果没有这种防火墙功能,攻击者就能将数据包发送给那些没有请求过的电话。

    这种网络地址转换方案造成两个移动电话之间无法通过TCP或UDP进行点对点的连接。当在移动网络上使用HTTP、TCP、UDP等协议时,各个电话一定需要通过一台互联网服务器,但它们之间并不能直接通信。

 

5. Demo

(1) Read the HTTP headers and the data using HttpConnection

Connector.open is used to open url and a HttpConnection is returned. The HTTP headers are read and processed. If the length is available, it is used to read the data in bulk. From the HttpConnection the InputStream is opened. It is used to read every character until end of file (-1) . If an exception is thrown the connection and stream are closed.

private void getViaHttpConnection(String url) throws IOException {
	HttpConnection c = null;
	InputStream is = null;
	int rc;
	try {
		c = (HttpConnection) Connector.open(url);
		
		/** 
		 * Getting the response code will open the connection, 
		 * send the request, and read the HTTP response headers. 
		 * The headers are stored until requested.
		 */
		rc = c.getResponseCode();
		if (rc != HttpConnection.HTTP_OK) {
			throw new IOException("Http response code: " + rc);
		}
		is = c.openInputStream();
		
		// Get the ContentType
		String type = c.getType();
		
		// Get the length and process the data
		int len = (int) c.getLength();
		if (len > 0) {
			int actual = 0;
			int bytesread = 0;
			byte[] data = new byte[len];
			while ((bytesread != len) && (actual != -1)) {
				// the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached. 
				actual = is.read(data, bytesread, len - bytesread);
				bytesread += actual;
			}
		} else {
			int ch;
			while ((ch = is.read()) != -1) {
				// ...
			}
		}
	} catch (ClassCastException e) {
		throw new IllegalArgumentException("Not an Http URL");
	} finally {
		if (is != null) is.close();
		if (c != null) c.close();
	}
}

 (2)Post a request with some headers and content to the server and process the headers and content

The request method is set to POST and request headers set. A simple command is written and flushed. The HTTP headers are read and processed. If the length is available, it is used to read the data in bulk. From the HttpConnection the InputStream is opened. It is used to read every character until end of file (-1) . If an exception is thrown the connection and stream is closed.

private void postViaHttpConnection(String url) throws IOException {
	HttpConnection c = null;
	InputStream is = null;
	OutputStream os = null;
	int rc;
	
	try {
		c = (HttpConnection) Connector.open(url);
		
		// Set the request method and headers
		c.setRequestMethod(HttpConnection.POST);
		c.setRequestProperty("If-Modified-Since", "29 Oct 1999 19:43:31 GMT");
		c.setRequestProperty("Content-Language", "en-US");
		
		// Getting the output stream may flush the headers
		os = c.openOutputStream();
		os.write("Forrest.He".getBytes());
		os.flush(); // Optional, getResponseCode will flush
		
		/** 
		 * Getting the response code will open the connection,
		 * send the request, and read the Http response headers.
		 * The headers are stored until requested.
		 */
		rc = c.getResponseCode();
		if (rc != HttpConnection.HTTP_OK) {
			throw new IOException("HTTP response code: " + rc);
		}
		is = c.openInputStream();
		// ... 同getViaHttpConnection()
		
	} catch (ClassCastException e) {
	} finally {
	}
}

 aa

分享到:
评论

相关推荐

    网络协议TCP/IP实验七 HTTP 协议分析实验

    实验选取了常见的HTTP协议作为研究对象,通过对HTTP协议的深入剖析,使学生能够掌握HTTP协议的基本原理、报文格式以及其工作流程。 #### 实验环境配置 - **操作系统**:Windows 7 - **网络平台**:实验室内部...

    认清http协议本质

    要认清HTTP协议的本质,我们需要从以下几个关键点入手: 1. 请求与响应模型:HTTP通信基于请求-响应模型。客户端发起一个HTTP请求到服务器,服务器处理请求并返回一个HTTP响应。请求由方法(如GET、POST)、URL、...

    pb通过http协议传json.zip

    pb通过http协议传json; pb通过http协议传json; pb通过http协议传json; pb通过http协议传json; pb通过http协议传json; pb通过http协议传json; pb通过http协议传json; pb通过http协议传json; pb通过http协议传json; pb...

    HTTP协议.zip_HTTP_HTTP协议

    **HTTP协议详解** HTTP(Hypertext Transfer Protocol)超文本传输协议是互联网上应用最广泛的一种网络协议。它定义了客户端(通常是Web浏览器)与服务器之间的通信格式,用于从万维网服务器传输超文本到本地浏览器...

    HTTP协议详解_HTTP协议_

    HTTP协议的工作原理: 1. 请求过程:当用户在浏览器中输入URL并按下回车键时,浏览器作为客户端(Client)构造一个HTTP请求报文,包括方法(GET、POST等)、URL、协议版本、头部信息和请求主体(如果有的话)。然后...

    HTTP协议详解(真的很经典)[归类].pdf

    HTTP 协议详解 HTTP 协议是一种应用层面的、面向对象的协议,用于分布式超媒体信息系统。它于 1990 年提出,经过多年的发展和完善,目前在 WWW 中使用的是 HTTP/1.0 的第六版,HTTP/1.1 的规范化工作正在进行中。 ...

    串口数据转换成http协议数据发送到云端

    在IT行业中,串口通信和HTTP协议是两个重要的领域,它们在各种硬件设备与网络服务之间建立起连接。本文将深入探讨如何将串口数据转换为HTTP协议数据,并将其发送到云端,以及源码软件的跨平台特性。 串口通信,也...

    HTTP协议详解及RFC2616(HTTP)中文版

    **HTTP协议详解** HTTP(Hypertext Transfer Protocol)超文本传输协议是互联网上应用最广泛的一种网络协议。它是用于从万维网服务器传输超文本到本地浏览器的传输协议,是Web应用的基础。HTTP协议定义了客户端...

    http协议中文版(http协议中文版)

    1. **无状态**:HTTP协议自身不维护连接状态,每次请求和响应都是独立的,服务器不会记住之前请求的信息。为了实现状态保持,开发者通常会使用Cookie或Session。 2. **简单快速**:设计时主要考虑快速传输,对数据...

    http协议分析工具

    HTTP协议分析工具能够帮助我们深入理解HTTP通信过程,包括请求方法、状态码、头部信息、请求体和响应体等关键元素。 首先,让我们看看JavaScript。JavaScript是一种轻量级的解释性编程语言,常用于网页和网络应用...

    计算机网络原理实验报告---Wireshark实验:HTTP协议分析

    本次实验是关于计算机网络原理的Wireshark工具使用,主要目标是对HTTP协议进行深入分析。实验过程中,我们使用Wireshark对电脑的WLAN端口进行抓包,观察并解析访问www.baidu.com网站时的网络通信过程。通过对HTTP...

    HTTP协议详解电子书

    HTTP协议详解电子书 HTTP(超文本传输协议)是互联网上应用最为广泛的一种网络协议,它的设计目的是为了传输数据,并且使数据交换变得简单、高效。HTTP协议是基于TCP/IP通信协议来传输数据的,主要负责客户端...

    基于http协议的自定义协议封装

    本文将深入探讨基于HTTP协议的自定义协议封装,特别是在使用XML作为数据载体时如何进行设计和实现。HTTP(超文本传输协议)是互联网上应用最为广泛的一种网络协议,它允许客户端(如浏览器)和服务器之间交换数据。...

    http协议中英文版

    本篇将基于提供的标题和描述,深入探讨HTTP协议的基本概念、主要特性以及RFC文档的相关知识。 首先,RFC(Request for Comments)是互联网工程任务组(IETF)发布的技术规范,用于记录和传播互联网相关的协议、标准...

    深入理解HTTP协议

    深入理解HTTP协议

    HTTP协议分析报告

    ### HTTP协议分析报告知识点梳理 #### 一、HTTP协议概述 **HTTP**(HyperText Transfer Protocol)是一种用于定义web页面(超文本)在网络上的交互方式的应用层协议,它使用客户/服务器的工作模式。HTTP协议规定了web...

    HTTP协议详解.pdf

    HTTP 协议详解 HTTP 协议是超文本传输协议(HyperText Transfer Protocol)的缩写,是目前万维网(World Wide Web)上最流行的通信协议。它是万维网的基础协议,定义了客户端和服务器之间的通信规则。 协议详解 #...

    HTTP协议抓包工具

    HTTP协议是互联网上应用最为广泛的一种网络协议,用于从万维网服务器传输超文本到本地浏览器的传输协议。抓包工具则是网络分析的重要辅助手段,它可以帮助我们查看网络中的数据包,了解网络通信的细节。在学习和研究...

Global site tag (gtag.js) - Google Analytics