关于java程序和http
本文导语: 怎样能在一个java写的socket web client 的程序里的get request加如If-modified-since的条件?又如何用If-modified-since和If-none-modified-since? 另外怎么才能把request header和respond header的信息用java socket client程序显示在dos下。 ...
另外怎么才能把request header和respond header的信息用java socket client程序显示在dos下。
|
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Info extends HttpServlet {
ServletConfig config;
public void init(ServletConfig tconfig) throws ServletException {
super.init(tconfig);
config=tconfig;}
public String getServletInfo() {
return "Info version 1.9: a demo and diagnostic servlet from vqSoft";}
private void pp(PrintWriter os, String name, String value) {
os.print(""+name+": ");
if (value==null)
os.print("none");
else
os.print(value);
os.println("
");}
private void pp(PrintWriter os, String name, int value) {
pp(os, name, String.valueOf(value));}
public void dostuff(HttpServletRequest req, HttpServletResponse rep) throws ServletException, IOException {
log("invoked");
rep.setContentType("text/html");
PrintWriter os=rep.getWriter();
os.println("");
os.println("Info servlet");
os.println("");
os.println("");
os.println("");
os.println("Info servlet");
os.println("");
os.println("
This page was generated by Info version 1.9, a demo and diagnostic servlet supplied with vqServer. ");
os.println("Info lists its initialisation parameters, information about the http request which invoked it and the http request parameters passed to it.
os.println("");
os.println("Servlet initialisation parameters");
Enumeration e=config.getInitParameterNames();
if (!e.hasMoreElements())
os.println("None!");
else while (e.hasMoreElements()) {
String name=(String) e.nextElement();
String value=config.getInitParameter(name);
pp(os, name, value);}
os.println("");
os.println("Request parameters");
pp(os, "Request method", req.getMethod());
pp(os, "Request URI", req.getRequestURI());
pp(os, "Request protocol", req.getProtocol());
pp(os, "Servlet path", req.getServletPath());
pp(os, "Path info", req.getPathInfo());
pp(os, "Path translated", req.getPathTranslated());
pp(os, "Query string", req.getQueryString());
pp(os, "Content length", req.getContentLength());
pp(os, "Content type", req.getContentType());
pp(os, "Server name", req.getServerName());
pp(os, "Server port", req.getServerPort());
pp(os, "Remote user", req.getRemoteUser());
pp(os, "Remote address", req.getRemoteAddr());
pp(os, "Remote host", req.getRemoteHost());
pp(os, "Authorization scheme", req.getAuthType());
os.println("");
os.println("Request headers");
e=req.getHeaderNames();
if (!e.hasMoreElements())
os.println("None!");
else while (e.hasMoreElements()) {
String name=(String) e.nextElement();
String value=(String) req.getHeader(name);
pp(os, name, value);}
os.println("");
os.println("Servlet parameters");
e=req.getParameterNames();
if (!e.hasMoreElements())
os.println("None!");
else while (e.hasMoreElements()) {
String name=(String) e.nextElement();
String value=req.getParameter(name);
StringBuffer tbuffer=new StringBuffer();
pp(os, name, value);}
os.println("");
os.println("Other servlets (by name)");
ServletContext tcontext=config.getServletContext();
e=tcontext.getServletNames();
if (!e.hasMoreElements())
os.println("None!");
else while (e.hasMoreElements()) {
os.println((String) e.nextElement()+"
");}
os.println("");
os.println("Other servlets");
e=tcontext.getServlets();
if (!e.hasMoreElements())
os.println("None!");
else while (e.hasMoreElements()) {
Servlet tservlet=(Servlet) e.nextElement();
os.println(tservlet.getServletInfo()+"
");}
os.println("");
os.println("
Info version 1.9. Info and vqServer are copyright © 1997-99 Steve Shering and vqSoft.");
os.println("");
os.println("");
os.println("");
os.println("Home page");
os.println("");
os.println("");
os.println("");
os.flush();}
public void doGet(HttpServletRequest req, HttpServletResponse rep) throws ServletException, IOException {
dostuff(req, rep);}
public void doPost(HttpServletRequest req, HttpServletResponse rep) throws ServletException, IOException {
dostuff(req, rep);}}
---------------
shmilu@sina.com
|
import java.net.*;
import java.io.*;
import java.util.*;
public class MIMEHeadersViewer {
public static void main(String args[]) {
for (int i=0; i