标签:
在上次Java Socket现实简单的HTTP服务我 们实现了简单的HTTP服务,它可以用来模拟HTTP服务,用它可以截获HTTP请求的原始码流,让我们很清楚的了解到我们向服务发的HTTP消息的结 构,对HTTP请求消息有个清晰的认识。这一节我想写了一个客户的程序,就是用来模拟浏览器,用来向服务器发送HTTP请求,最得要的是可以用它来显示服 务器发回来的HTTP响应消息的一般结构。
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStreamWriter;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.util.ArrayList;
-
- public class SimpleHttpClient {
- private static String encoding = "GBK";
-
- public static void main(String[] args) {
- try {
- Socket s = new Socket(InetAddress.getLocalHost(), 8080);
- OutputStreamWriter osw = new OutputStreamWriter(s.getOutputStream());
- StringBuffer sb = new StringBuffer();
- sb.append("GET /HttpStream/gb2312.jsp HTTP/1.1\r\n");
- sb.append("Host: localhost:8088\r\n");
- sb.append("Connection: Keep-Alive\r\n");
-
- sb.append("\r\n");
- osw.write(sb.toString());
- osw.flush();
-
-
- InputStream is = s.getInputStream();
- String line = null;
- int contentLength = 0;
-
- do {
- line = readLine(is, 0);
-
- if (line.startsWith("Content-Length")) {
- contentLength = Integer.parseInt(line.split(":")[1].trim());
- }
-
- System.out.print(line);
-
- } while (!line.equals("\r\n"));
-
-
- System.out.print(readLine(is, contentLength));
-
-
- is.close();
-
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
-
- private static String readLine(InputStream is, int contentLe) throws IOException {
- ArrayList lineByteList = new ArrayList();
- byte readByte;
- int total = 0;
- if (contentLe != 0) {
- do {
- readByte = (byte) is.read();
- lineByteList.add(Byte.valueOf(readByte));
- total++;
- } while (total < contentLe);
- } else {
- do {
- readByte = (byte) is.read();
- lineByteList.add(Byte.valueOf(readByte));
- } while (readByte != 10);
- }
-
- byte[] tmpByteArr = new byte[lineByteList.size()];
- for (int i = 0; i < lineByteList.size(); i++) {
- tmpByteArr[i] = ((Byte) lineByteList.get(i)).byteValue();
- }
- lineByteList.clear();
-
- return new String(tmpByteArr, encoding);
- }
- }
运行时访问一个页面打印如下:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=61F659691475622CE7AB9C84E7AE7273; Path=/HttpStream
Content-Type: text/html;charset=GB2312
Content-Length: 81
Date: Mon, 09 Nov 2009 13:15:23 GMT
<html>
<body>
你好,这是一个简单的测试
</body>
</html>
|
下面来个文件下载的看怎么样?
请求的Jsp页面如下:
- <%@page import="java.io.InputStream" contentType="text/html; charset=GB2312"%>
- <%@page import="java.io.FileInputStream"%>
-
- <%@page import="java.io.OutputStream"%><html>
- <body> <br>
- <%
- try {
- InputStream is = new FileInputStream("e:/tmp/file2.txt");
- OutputStream os = response.getOutputStream();
- byte[] readContent = new byte[1024];
- int readCount = 0;
- while (is.available() > 0) {
- readCount = is.read(readContent);
- os.write(readContent, 0, readCount);
- }
-
- is.close();
-
-
-
-
- os.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- %>
- </body>
- </html>
如里上面Jsp下载页面中的 os.close() 注释掉的话会抛如下异常:
exception
org.apache.jasper.JasperException: getOutputStream() has already been called for this response
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:383)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
org.apache.catalina.connector.Response.getWriter(Response.java:601)
org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:196)
org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:185)
org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:116)
org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:76)
org.apache.jsp.gb2312_jsp._jspService(gb2312_jsp.java:78)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
以下是服务器经过编译生成的servlet类文件:
- package org.apache.jsp;
-
- import javax.servlet.*;
- import javax.servlet.http.*;
- import javax.servlet.jsp.*;
- import java.io.InputStream;
- import java.io.FileInputStream;
- import java.io.OutputStream;
-
- public final class gb2312_jsp extends org.apache.jasper.runtime.HttpJspBase
- implements org.apache.jasper.runtime.JspSourceDependent {
-
- private static java.util.List _jspx_dependants;
-
- public Object getDependants() {
- return _jspx_dependants;
- }
-
- public void _jspService(HttpServletRequest request, HttpServletResponse response)
- throws java.io.IOException, ServletException {
-
- JspFactory _jspxFactory = null;
- PageContext pageContext = null;
- HttpSession session = null;
- ServletContext application = null;
- ServletConfig config = null;
- JspWriter out = null;
- Object page = this;
- JspWriter _jspx_out = null;
- PageContext _jspx_page_context = null;
-
-
- try {
- _jspxFactory = JspFactory.getDefaultFactory();
- response.setContentType("text/html; charset=GB2312");
- pageContext = _jspxFactory.getPageContext(this, request, response,
- null, true, 8192, true);
- _jspx_page_context = pageContext;
- application = pageContext.getServletContext();
- config = pageContext.getServletConfig();
- session = pageContext.getSession();
- out = pageContext.getOut();
- _jspx_out = out;
-
- out.write("\r\n");
- out.write("\r\n");
- out.write("\r\n");
- out.write("<html>\r\n");
- out.write("\t<body> <br>\r\n");
- out.write("\t\t");
-
- try {
- InputStream is = new FileInputStream("e:/tmp/file2.txt");
- OutputStream os = response.getOutputStream();
- byte[] readContent = new byte[1024];
- int readCount = 0;
- while (is.available() > 0) {
- readCount = is.read(readContent);
- os.write(readContent, 0, readCount);
- }
-
- is.close();
-
-
-
-
- os.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- out.write("\r\n");
- out.write("\t</body>\r\n");
- out.write("</html>");
- } catch (Throwable t) {
- if (!(t instanceof SkipPageException)){
- out = _jspx_out;
- if (out != null && out.getBufferSize() != 0)
- out.clearBuffer();
- if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
- }
- } finally {
- if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
- }
- }
- }
最后是服务向客户端输出的码流如下:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=328097D70C625E8A9279FF9472319A5D; Path=/HttpStream
Content-Type: text/html;charset=GB2312
Content-Length: 60
Date: Mon, 09 Nov 2009 13:19:22 GMT
这是测试文件的内容:
中a ~!@#$%^&*()_+{}|:\" <>?`-=[]\\;‘,./
原文:http://blog.csdn.net/a9529lty/article/details/7174265
Java Socket发送与接收HTTP消息简单实现
标签:
原文地址:http://www.cnblogs.com/langtianya/p/4329081.html