URLConnection发送HTTP请求的方法_动力节点Java学院整理

  

如何通过Java发送HTTP请求,通俗点讲,如何通过Java(模拟浏览器)发送HTTP请求。
  

  

Java有原生的API可用于发送HTTP请求,即java.net.URL, java.net.URLConnection,这些API很好用,很常用,但不够简便,
  所以,也流行有许多Java HTTP请求的框架,如Apache的HttpClient。
  

  

目前项目主要用Java原到生的方式,所以,这里主要介绍此方式。
  

  

运用原生Java Api发送简单的就请求,帖子请求
  

  

HTTP请求粗分为两种,一种是得到请求,一种是后请求。使用Java发送这两种请求的代码大同小异,只是一些参数设置的不同。步骤如下:
  

  

1。通过统一资源定位器(java.net.URL)获取连接器(java.net.URLConnection)
  

  

2。设置请求的参数
  

  

3。发送请求
  

  

4。以输入流的形式获取返回内容
  

  

5。关闭输入流

  

<强>简单的得到请求示例如下:

        包com.bjpowernode.httprequestdemo;      进口java.io.BufferedReader;   进口java.io.InputStream;   进口java.io.InputStreamReader;   进口java.net.HttpURLConnection;   进口java.net.URL;   进口java.net.URLConnection;      公开课HttpGetRequest {/* *   *主要   * @param arg游戏   * @throws例外   */公共静态void main (String [] args){抛出异常   System.out.println (doGet ());   }/* *   * Get请求   * @return   * @throws例外   */公共静态字符串doGet()抛出异常{   URL localURL=新的URL (http://localhost: 8080/OneHttpServer/);   URLConnection连接=localURL.openConnection ();   HttpURLConnection HttpURLConnection=(HttpURLConnection)连接;      httpURLConnection。setRequestProperty (“Accept-Charset”、“utf - 8”);   httpURLConnection。setRequestProperty(“内容类型”,“应用程序/x-www-form-urlencoded”);      InputStream InputStream=零;   InputStreamReader InputStreamReader=零;   BufferedReader读者=零;   StringBuffer resultBuffer=new StringBuffer ();   字符串tempLine=零;      如果(httpURLConnection.getResponseCode()在=300){   抛出新的异常(“HTTP请求不成功,响应代码”+ httpURLConnection.getResponseCode ());   }      尝试{   inputStream=httpURLConnection.getInputStream ();   inputStreamReader=new inputStreamReader (inputStream);   读者=new BufferedReader (inputStreamReader);      在((tempLine=reader.readLine ()) !=null) {   resultBuffer.append (tempLine);   }      最后}{      如果(读者!=null) {   reader.close ();   }      如果(inputStreamReader !=null) {   inputStreamReader.close ();   }      如果(inputStream !=null) {   inputStream.close ();   }      }      返回resultBuffer.toString ();   }      }      之前      

<强>,简单的帖子请求示例如下:

        包com.bjpowernode.httprequestdemo;      进口java.io.BufferedReader;   进口java.io.InputStream;   进口java.io.InputStreamReader;   进口java.io.OutputStream;   进口java.io.OutputStreamWriter;   进口java.net.HttpURLConnection;   进口java.net.URL;   进口java.net.URLConnection;      公开课HttpPostRequest {/* *   *主要   * @param arg游戏   * @throws例外   */公共静态void main (String [] args){抛出异常   System.out.println (doPost ());   }/* *   * Post请求   * @return   * @throws例外   */公共静态字符串doPost()抛出异常{   字符串parameterData=" https://www.yisu.com/zixun/username=nickhuang&blog=http://www.cnblogs.com/nick-huang/?      URL localURL=新的URL (http://localhost: 8080/OneHttpServer/);   URLConnection连接=localURL.openConnection ();   HttpURLConnection HttpURLConnection=(HttpURLConnection)连接;      httpURLConnection.setDoOutput(真正的);   httpURLConnection.setRequestMethod(“文章”);   httpURLConnection。setRequestProperty (“Accept-Charset”、“utf - 8”);   httpURLConnection。setRequestProperty(“内容类型”,“应用程序/x-www-form-urlencoded”);   httpURLConnection。内容长度setRequestProperty (“”, String.valueOf (parameterData.length ()));      OutputStream OutputStream=零;   OutputStreamWriter OutputStreamWriter=零;   InputStream InputStream=零;   InputStreamReader InputStreamReader=零;   BufferedReader读者=零;   StringBuffer resultBuffer=new StringBuffer ();   字符串tempLine=零;      尝试{   outputStream=httpURLConnection.getOutputStream ();   outputStreamWriter=new outputStreamWriter (outputStream);      outputStreamWriter.write (parameterData.toString ());   outputStreamWriter.flush ();      如果(httpURLConnection.getResponseCode()在=300){   抛出新的异常(“HTTP请求不成功,响应代码”+ httpURLConnection.getResponseCode ());   }      inputStream=httpURLConnection.getInputStream ();   inputStreamReader=new inputStreamReader (inputStream);   读者=new BufferedReader (inputStreamReader);      在((tempLine=reader.readLine ()) !=null) {   resultBuffer.append (tempLine);   }      最后}{      如果(outputStreamWriter !=null) {   outputStreamWriter.close ();   }      如果(outputStream !=null) {   outputStream.close ();   }      如果(读者!=null) {   reader.close ();   }      如果(inputStreamReader !=null) {   inputStreamReader.close ();   }      如果(inputStream !=null) {   inputStream.close ();   }      }      返回resultBuffer.toString ();   }      }      

URLConnection发送HTTP请求的方法_动力节点Java学院整理