在JAVA中发送HTTP请求的方式有哪些

  介绍

在JAVA中发送HTTP请求的方式有哪些?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

JAVA是什么

JAVA是一门面向对象编程语言,可以编写桌面应用程序,网络应用程序,分布式系统和嵌入式系统应用程序。

1。HttpURLConnection

使用JDK原生提供的网,无需其他jar包;

HttpURLConnection是URLConnection的子类,提供更多的方法,使用更方便。

package  HttpURLConnection;      import  java.io.BufferedReader;   import  java.io.InputStream;   import  java.io.InputStreamReader;      import  java.net.HttpURLConnection;   import  java.net.URL;      public  class  HttpURLConnectionHelper  {      ,public  static  String  sendRequest (String  urlParam String  requestType), {      HttpURLConnection 才能;con =,零,,      BufferedReader 才能;buffer =,零,,   StringBuffer 才能;resultBuffer =,零,,      try {才能   ,,URL  URL =, new  URL (urlParam);,   ,,//得到连接对象   ,,con =, (HttpURLConnection), url.openConnection (),,   ,,//设置请求类型   ,,con.setRequestMethod (requestType);,   ,,//设置请求需要返回的数据类型和字符集类型   ,,con.setRequestProperty (“Content-Type",,“application/json; charset=GBK"),,   ,,//允许写出   ,,con.setDoOutput(真正的);   ,,//允许读入   ,,con.setDoInput(真正的);   ,,//不使用缓存   ,,con.setUseCaches(假);   ,,//得到响应码   ,,int  responseCode =, con.getResponseCode ();      ,,如果(responseCode ==, HttpURLConnection.HTTP_OK) {   ,,,//得到响应流   ,,,InputStream  InputStream =, con.getInputStream ();   ,,,//将响应流转换成字符串   ,,,resultBuffer =, new  StringBuffer ();   ,,,String 行;   ,,,buffer =, new  BufferedReader (new  InputStreamReader (inputStream,“GBK"));   ,,,while  ((=line  buffer.readLine ()), !=, null), {   ,,,,resultBuffer.append(线);   ,,,}   ,,,return  resultBuffer.toString ();   ,,}      }才能赶上(Exception  e), {   ,,e.printStackTrace ();   ,,}   return “才能”;   ,}   ,public  static  void  main (String [], args), {      String 才能;url =? http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=120.79.75.96";   System.out.println才能(sendRequest (url,“POST"));   ,}   }

2。URLConnection

使用JDK原生提供的网,无需其他jar包;

建议使用HttpURLConnection

package  URLConnection;      import  java.io.BufferedReader;   import  java.io.InputStream;   import  java.io.InputStreamReader;   import  java.net.HttpURLConnection;   import  java.net.URL;   import  java.net.URLConnection;      public  class  URLConnectionHelper  {      ,public  static  String  sendRequest (String  urlParam), {      URLConnection 才能;con =,零,,      BufferedReader 才能;buffer =,零,,   StringBuffer 才能;resultBuffer =,零,,      try {才能   ,,,URL  URL =, new  URL (urlParam);,   ,,,con =, url.openConnection (),,      ,,//设置请求需要返回的数据类型和字符集类型   ,,con.setRequestProperty (“Content-Type",,“application/json; charset=GBK"),,   ,,//允许写出   ,,con.setDoOutput(真正的);   ,,//允许读入   ,,con.setDoInput(真正的);   ,,//不使用缓存   ,,con.setUseCaches(假);   ,,//得到响应流   ,,InputStream  InputStream =, con.getInputStream ();   ,,//将响应流转换成字符串   ,,resultBuffer =, new  StringBuffer ();   ,,String 线;   ,,buffer =, new  BufferedReader (new  InputStreamReader (inputStream,“GBK"));   ,,while  ((=line  buffer.readLine ()), !=, null), {   ,,,resultBuffer.append(线);   ,,}   ,,return  resultBuffer.toString ();      }才能赶上(Exception  e), {   ,,e.printStackTrace ();   ,,}      return “才能”;   ,}   ,public  static  void  main (String [], args), {   String 才能;url =? http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=120.79.75.96";   System.out.println才能(sendRequest (url));   ,}   }

在JAVA中发送HTTP请求的方式有哪些