您的位置:首页 > 理论基础 > 计算机网络

http

2015-10-12 20:55 387 查看
Http模式:

以Http协议在服务端和客户端之间通信

服务端:

首先,需要在Tomcat的安装文件下的conf目录下的server.xml文件的Host节点下添加



java web server,在MyEclipse上面;建立web server工程,配置Tomcat配置方法参考网站http://wenku.baidu.com/link?url=AGUpl3gSSnt3m4gM8RFsDUvgTqX8SE2XR_9Gf1fTEe2fuJvN16Vw8xTIEVkE9-z_qsW0Ti6niClUQgYgcUzF3rekOqH_aiLm_jXYVIadWA7

建好工程后,把需要获取的文件放在WebRoot下即可,最后Server下启动Tomcat即可

Android客户端的http(使用标准的java接口):

1. 以“GET”形式从服务器获取数据

确定资源定位符==>>打开连接==》》设置连接属性==》》获取服务器数据==》》后续对数据处理

URL url = new URL(URL_Path); //URL_Path可以是这样”http://192.168.1.102:8080/http_server/1.jpg” http_server是服务端的工程名,1.jpg是在工程下WebRoot下的

[code]HttpURLConnection httpURLConnection = null;


httpURLConnection = (HttpURLConnection)url.openConnection(); //获取http连接

//设置连接属性

httpURLConnection.setConnectTimeout(3000); //3秒超时

httpURLConnection.setDoInput(true); //此连接(connection)是否允许输入

httpURLConnection.setRequestMethod(“GET”); //将此命令发送个server,连接前调用,以GET形式获取数据

int state = httpURLConnection.getResponseCode(); //获取返回码,如果成功,就会得到一个流

if(state == HttpURLConnection.HTTP_OK){

//从服务器获取一个输入流

inputStream = httpURLConnection.getInputStream();

}

一般从服务器获取下来的都是以流的形式存在的,需要进行转换

流存放到磁盘; inputstream= >byte[]==>fileoutputstream

inputstream.read(byte[]); //read表示在流上读,那就是读出来了,返回值是读取数据的长度,读完为-1

fileoutputstream.write(byte,offset,length); //写到磁盘上,以文件形式;

2.以“POST”形式向服务器发送数据

确定连接的URL资源定位符==》打开连接connection==》准备好数据流(ouputstream)==>>获取返回码和数据流(inputStream)

打开资源定位符

private static String PATH = “http://192.168.1.102:8080/http_server/servlet/LoginAction“;

url = new URL(PATH);

打开连接connection

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

httpURLConnection.setConnectTimeout(3000);

httpURLConnection.setDoInput(true); //使能获取服务器数据

httpURLConnection.setDoOutput(true); //使能向服务器发送数据

//请求体的类型设置 即将自己要发送的数据类型和长度

httpURLConnection.setRequestProperty(“Content”, “application/x-www-formurlencoded”); //文本类型

httpURLConnection.setRequestProperty(“Content-Length”, String.valueOf(data.length)); //文本长度

传输的数据:

事先先准备好数据,然后从连接中获取输出流

outputstream = httpURLConnection.getOutputStream();

outputStream.write(data);

获取数据:

int state = httpURLConnection.getResponseCode();

if(state == HttpURLConnection.HTTP_OK){

InputStream inputStream = httpURLConnection.getInputStream();

result = inputStreamToString(inputStream,encode); //将output流转换为字符串,encode是字符编码,一般为utf-8

}

POST服务端方面:

需重写doPost方法,在HttpServlet类里面,继承它即可

protected void doPost(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

// TODO Auto-generated method stub

resp.setContentType(“text/html;charset=utf-8”); //设置文本类型,主要是utf-8

req.setCharacterEncoding(“utf-8”); //设置编码

resp.setCharacterEncoding(“utf-8”);

PrintWriter out = resp.getWriter(); //向客户端发送的数据对象

String username = req.getParameter(“username”); //获取用户名

System.out.println(“username=”+username);

String password = req.getParameter(“password”); //获取密码

System.out.println(“password=”+password);

if(username.equals(“admain”) && password.equals(“1234”)){ //响应操作

out.print(“Login Success!”);

}else{

out.print(“Login failed!”);

}

out.flush();

out.close(); //刷缓冲,关闭流

}

还需注意,在WebRoot目录下的/WEB-INF/web.xml目录下要添加servlet服务,这样客户端才能连接上来



this is the description of my J2EE Commponent

this is the displayname of my J2EE Commponent

LoginAction

com.http.Login.LoginAction





LoginAction

/servlet/LoginAction //这里面的复制到url的后面,好好比较下



Android客户端的http(使用标准的Apache接口):

开发步骤: 提交的数据封装到容器List ==》 数据封装到表单(请求体中UrlEncodedFormEntity ) ==》 建立Httppost,

并加入请求体==》配置http客户端DefaultHttpClient ==》将httppost加入客户端中,同时获取返回值httpresponse,并在httpresponse中获

取服务端回来的inputstream,
这里写代码片
转换为string即可

public static String sendHttpClientPOSTMsg(String path,Map
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: