展会信息港展会大全

Android版Web服务器实现(三)HTTP响应
来源:互联网   发布日期:2015-09-28 15:44:55   浏览:1538次  

导读:《Android版Web服务器实现(二)使用服务来监听HTTP请求》一文实现了HTTP请求的监听,那么我们要如何作出响应呢?在响应时,有几种情况。1、请求的方法不支持。比如服务端仅支持了GE...

《Android版Web服务器实现(二)使用服务来监听HTTP请求》一文实现了HTTP请求的监听,那么我们要如何作出响应呢?在响应时,有几种情况。

1、请求的方法不支持。比如服务端仅支持了GET/POST方法,而请求却有DELETE等,此时回复501。

2、请求的资源不存在。在服务端不存在该资源文件,将回复404页面。

3、请求的类型不支持。服务端可能存在该资源,但是该资源的类型没有支持,将回复404.7。

4、请求正常。服务端将相应的资源回复给客户端。

5、其他情况。

下面是依据这些情况的代码实现。

SessionThread.java

package com.sparkle.webservice;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.Socket;

import android.util.Log;

public class SessionThread extends Thread {

private Socket _clientSocket = null;

private final int BUFFER_MAX = 8192;

private DataHandle _dataHandle = null;

private MyLog _myLog = new MyLog(getClass().getName());

public SessionThread(Socket clientSocket) {

this._clientSocket = clientSocket;

}

public void closeSocket() {

if (_clientSocket == null) {

return;

}

try {

_clientSocket.close();

} catch (IOException e) {

_myLog.e(e.getMessage());

}

}

public void run() {

try {

InputStream socketInput = _clientSocket.getInputStream();

byte[] buffer = new byte[BUFFER_MAX];

socketInput.read(buffer);

_dataHandle = new DataHandle(buffer);

byte[] content = _dataHandle.fetchContent();

sendResponse(_clientSocket, content);

} catch (Exception e) {

_myLog.l(Log.DEBUG, "Exception in TcpListener");

}

}

private void sendResponse(Socket clientSocket, byte[] content) {

try {

OutputStream socketOut = clientSocket.getOutputStream();

byte[] header = _dataHandle.fetchHeader(content.length);

socketOut.write(header);

socketOut.write(content);

socketOut.close();

clientSocket.close();

} catch (Exception e) {

}

}

}

注:

1、在收到请求后,新建一个SessionThread来处理请求(在上一篇中有说到)。

2、SessionThread建立后,在run中新建DataHandle来处理数据,具体请参看后文代码。

3、在sendResponse来回应请求。

4、在响应时,先发送header,再发送content来响应

DataHandle.java

package com.sparkle.webservice;

import java.io.UnsupportedEncodingException;

import com.sparkle.io.FileSp;

import android.annotation.SuppressLint;

import android.util.Log;

public class DataHandle {

private String _receiveInfo = "";

private HttpHeader _httpHeader = null;

private String _encoding = "utf-8";

private String _serverName = "Simple Web Server";

private String _responseCode = "200 OK";

private String _contentType = "text/html";

public DataHandle(byte[] recieveData) {

try {

this._receiveInfo = new String(recieveData, _encoding);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

_httpHeader = new HttpHeader(_receiveInfo);

}

public byte[] fetchContent() {

byte[] backData = null;

if (!isSupportMethod()) {

backData = fetchNotSupportMethodBack();

return backData;

}

String filePath = fetchFilePath();

boolean hasFile=FileSp.isExist(filePath);

Log.e("FilePath", filePath+""+hasFile);

if (!hasFile) {

backData = fetchNotFoundBack();

return backData;

}

if (!isSupportExtension()) {

backData = fetchNotSupportFileBack();

return backData;

}

backData = fetchBackFromFile(filePath);

return backData;

}

public byte[] fetchHeader(int contentLength) {

byte[] header = null;

try {

header = ("HTTP/1.1 " + _responseCode + "\r\n"

+ "Server: "+ _serverName + "\r\n"

+ "Content-Length: " + contentLength+ "\r\n"

+ "Connection: close\r\n"

+ "Content-Type: "+ _contentType + ";charset="+_encoding+"\r\n\r\n").getBytes(_encoding);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return header;

}

@SuppressLint("DefaultLocale")

private boolean isSupportMethod() {

String method = _httpHeader.getMethod();

if (method == null || method.length() "

+ _serverName

+ "

501 - Method Not Implemented

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港