服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

一个用来访问http服务器的东西。功能类似于java.net中的那个。但要强。


  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();

   

扫描关注微信公众号