当前位置: 移动技术网 > IT编程>开发语言>Java > java发送http的get、post请求实现代码

java发送http的get、post请求实现代码

2019年07月22日  | 移动技术网IT编程  | 我要评论
http请求类 package wzh.http; import java.io.bufferedreader; import java.io.ioexc

http请求类

package wzh.http;

import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.printwriter;
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;
  }  
}

调用方法:

public static void main(string[] args) {
    //发送 get 请求
    string s=httprequest.sendget("http://localhost:6144/home/requeststring", "key=123&v=456");
    system.out.println(s);
    
    //发送 post 请求
    string sr=httprequest.sendpost("http://localhost:6144/home/requestpoststring", "key=123&v=456");
    system.out.println(sr);
  }

以上这篇java发送http的get、post请求实现代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网