网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  一个用来访问http服务器的东西。功能类似于java.net中的那个。但要强。     
  文章作者:未知  文章来源:水木森林  
  查看:92次  录入:管理员--2007-11-17  
 
  package net.sonyhome.net;

import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
/**
* 一个用来访问http服务器的东西。功能类似于java.net中的那个。但要强,这个对post方法的支持更好。
* 其实也不能说是我写的。不记得从哪儿找来的程序,稍事修改了一下。所以现在程序的结构都忘啦。
* 不过有一点是肯定的,那就是可以用。呵呵。
* 去年我做的java版的精华区就是用这个类来访问的。
* creation date: (2001-8-24 23:57:14)
* @author: sonymusic
*/
public class httpconnection {
    private url url = null;
    //private boolean doinput = true;
    //private boolean dooutput = true;

    private boolean usepost = false;

    private boolean usecaches = false;

    private vector reqheadernames = new vector();
    private vector reqheadervalues = new vector();
    private vector resheadernames = null;
    private vector resheadervalues = null;
    private socket socket = null;
    private outputstream out = null;
    private inputstream in = null;
    private boolean usehttp11 = false;

    private boolean connected = false;

    private boolean inputstarted = false;

    hashtable postdata = new hashtable();
    hashtable getdata = new hashtable();

    /**
     * httpconnection constructor comment.
     */
    public httpconnection(url url) {
        super();
        this.url = url;
    }
    /**
     * insert the method's description here.
     * creation date: (2001-8-25 1:16:52)
     * @param name java.lang.string
     * @param value java.lang.string
     */
    public void addget(string name, string value) {
        getdata.put(name, value);
    }
    /**
     * insert the method's description here.
     * creation date: (2001-8-25 1:16:52)
     * @param name java.lang.string
     * @param value java.lang.string
     */
    public void addpost(string name, string value) {
        postdata.put(name, value);
    }
    public void close() throws ioexception {
        if (!connected)
            return;
        out.close();
        if (inputstarted)
            in.close();
        socket.close();
    }
    public void connect() throws ioexception {
        if (connected)
            return;
        if (!usecaches) {
            setrequestproperty("pragma", "no-cache");
            //setrequestproperty("cache-control", "no-cache, must-revalidate");
            //setrequestproperty("expires", "mon, 26 jul 1997 05:00:00 gmt");
        }
        string protocol = url.getprotocol();
        if (!protocol.equals("http"))
            throw new unknownserviceexception("unknown protocol");
        string host = url.gethost();
        int port = url.getport();
        if (port == -1)
            port = 80;
        string file = url.getfile();

        socket = new socket(host, port);
        out = socket.getoutputstream();
        printstream pout = new printstream(out);

        string method;
        if (usepost) {
            method = "post";
            setrequestproperty("content-type", "application/x-www-form-urlencoded");
            int len = getpostdatalength();
            setrequestproperty("content-length", string.valueof(getpostdatalength()));

        }
        else
            method = "get";
        if (getgetdatalength() > 0) {
            file += "?" + getgetdatastring();
        }
        pout.println(method + " " + file + " http/1.0");

        for (int i = 0; i < reqheadernames.size(); ++i) {
            string name = (string) reqheadernames.elementat(i);
            string value = (string) reqheadervalues.elementat(i);
            pout.println(name + ": " + value);
        }
        pout.println("");
        if (usepost) {
            string ttt = getpostdatastring();
            pout.println(getpostdatastring());
        }

        pout.flush();

   
 
 
上一篇: 一个经典的java applet时钟程序(一)    下一篇: 一些非常有用的java常用方法( 2)
  相关文档
java nio原理和使用 11-17
最新消息:hibernate 3.1发布 11-17
java基础篇 初学者入门必备的学习资料 11-16
java入门(5) java语言基本语法 11-17
xml带来的好处 11-17
mvc减少编程复杂性 11-16
scjp认证考试全接触 11-17
预装载以及javascript image()对象 11-16
skip 方法 11-16
在java中轻松打印文档 11-16
java中引用,造型等问题解析 11-17
j2me综合--j2me应用程序内存优化三招 02-27
通过Java Swing看透MVC设计模式 08-06
jdom入门:使用jdom解析xml实例详解 11-16
用jface和swt构建简单的应用程序(2) 11-17
oracle rac on sun cluster 3.1(step by step) 11-17
Java操作Excel之理解JXL---读取Excel 04-14
在j2me游戏使用“精灵”-sprite(图) 11-17
hibernate二级缓存攻略 11-17
让java程序带着jre一起上路 11-16
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息