欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java网络编程中向指定URL发送GET POST请求示例

程序员文章站 2023-12-20 12:33:28
复制代码 代码如下:import java.io.bufferedreader;import java.io.ioexception;import java.io.inpu...
复制代码 代码如下:

import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.outputstream;
import java.io.printwriter;
import java.net.httpurlconnection;
import java.net.url;
import java.net.urlconnection;
import java.util.list;
import java.util.map;

public class httprequest {
    /**
     * 向指定url发送get方法的请求
     *
     * @param url
     *            发送请求的url
     * @param param
     *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
     * @return url 所代表远程资源的响应结果
     */
    public static string sendget(string url, string param) {
        string result = "";
        bufferedreader in = null;
        try {
            string urlnamestring = url + "?" + param;
            url realurl = new url(urlnamestring);
            // 打开和url之间的连接
            urlconnection connection = realurl.openconnection();
            // 设置通用的请求属性
            connection.setrequestproperty("accept", "*/*");
            connection.setrequestproperty("connection", "keep-alive");
            connection.setrequestproperty("user-agent",
                    "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1;sv1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            /*
             * map<string, list<string>> map = connection.getheaderfields(); //
             * 遍历所有的响应头字段 for (string key : map.keyset()) {
             * system.out.println(key + "--->" + map.get(key)); }
             */
            // 定义 bufferedreader输入流来读取url的响应
            in = new bufferedreader(new inputstreamreader(
                    connection.getinputstream()));
            string line;
            while ((line = in.readline()) != null) {
                result += line;
            }
        } catch (exception e) {
            system.out.println("发送get请求出现异常!" + e);
            e.printstacktrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (exception e2) {
                e2.printstacktrace();
            }
        }
        return result;
    }

    /**
     * 向指定 url 发送post方法的请求
     *
     * @param url
     *            发送请求的 url
     * @param param
     *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
     * @return 所代表远程资源的响应结果
     */
    public static string sendpost(string url, string param) {
        printwriter out = null;
        bufferedreader in = null;
        string result = "";
        try {
            url realurl = new url(url);
            // 打开和url之间的连接
            urlconnection conn = realurl.openconnection();
            // 设置通用的请求属性
            conn.setrequestproperty("accept", "*/*");
            conn.setrequestproperty("connection", "keep-alive");
            conn.setrequestproperty("user-agent",
                    "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1;sv1)");
            // 发送post请求必须设置如下两行
            conn.setdooutput(true);
            conn.setdoinput(true);
            // 获取urlconnection对象对应的输出流
            out = new printwriter(conn.getoutputstream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义bufferedreader输入流来读取url的响应
            in = new bufferedreader(
                    new inputstreamreader(conn.getinputstream()));
            string line;
            while ((line = in.readline()) != null) {
                result += line;
            }
        } catch (exception e) {
            system.out.println("发送 post 请求出现异常!" + e);
            e.printstacktrace();
        }
        // 使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (ioexception ex) {
                ex.printstacktrace();
            }
        }
        return result;
    }
}

上一篇:

下一篇: