zoukankan      html  css  js  c++  java
  • 通过java.net.URLConnection发送HTTP请求的方法

      1 package cn.smartercampus.cloud.core.util;
      2 
      3 import java.io.BufferedReader;
      4 import java.io.InputStream;
      5 import java.io.InputStreamReader;
      6 import java.io.OutputStream;
      7 import java.io.OutputStreamWriter;
      8 import java.net.HttpURLConnection;
      9 import java.net.URL;
     10 import java.net.URLConnection;
     11 
     12 
     13 
     14 public class myHttpUtil {
     15     
     16     public static void setUserInfo() throws Exception {
     17         doGet("http://localhost/api/setUserInfo_test");
     18     }
     19     
     20     public static void main(String[] args) throws Exception {
     21         System.out.println(doGet("http://localhost/api/queryForList/prepare.getMyCollectBeike"));
     22     }
     23     
     24     
     25     /**
     26      * Post Request
     27      * @return
     28      * @throws Exception
     29      */
     30     public static String doPost(String url) throws Exception {
     31         String parameterData = "test=test&2=2";
     32         
     33         URL localURL = new URL(url);
     34         URLConnection connection = localURL.openConnection();
     35         HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
     36         
     37         httpURLConnection.setDoOutput(true);
     38         httpURLConnection.setRequestMethod("POST");
     39         httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
     40         httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
     41         httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameterData.length()));
     42         
     43         OutputStream outputStream = null;
     44         OutputStreamWriter outputStreamWriter = null;
     45         InputStream inputStream = null;
     46         InputStreamReader inputStreamReader = null;
     47         BufferedReader reader = null;
     48         StringBuffer resultBuffer = new StringBuffer();
     49         String tempLine = null;
     50         
     51         try {
     52             outputStream = httpURLConnection.getOutputStream();
     53             outputStreamWriter = new OutputStreamWriter(outputStream);
     54             
     55             outputStreamWriter.write(parameterData.toString());
     56             outputStreamWriter.flush();
     57             
     58             if (httpURLConnection.getResponseCode() >= 300) {
     59                 throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
     60             }
     61             
     62             inputStream = httpURLConnection.getInputStream();
     63             inputStreamReader = new InputStreamReader(inputStream);
     64             reader = new BufferedReader(inputStreamReader);
     65             
     66             while ((tempLine = reader.readLine()) != null) {
     67                 resultBuffer.append(tempLine);
     68             }
     69             
     70         } finally {
     71             
     72             if (outputStreamWriter != null) {
     73                 outputStreamWriter.close();
     74             }
     75             
     76             if (outputStream != null) {
     77                 outputStream.close();
     78             }
     79             
     80             if (reader != null) {
     81                 reader.close();
     82             }
     83             
     84             if (inputStreamReader != null) {
     85                 inputStreamReader.close();
     86             }
     87             
     88             if (inputStream != null) {
     89                 inputStream.close();
     90             }
     91             
     92         }
     93 
     94         return resultBuffer.toString();
     95     }
     96 
     97     
     98     /**
     99      * Get Request
    100      * @return
    101      * @throws Exception
    102      */
    103     public static String doGet(String url) throws Exception {
    104         URL localURL = new URL(url);
    105         URLConnection connection = localURL.openConnection();
    106         HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
    107         
    108         httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
    109         httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    110         
    111         InputStream inputStream = null;
    112         InputStreamReader inputStreamReader = null;
    113         BufferedReader reader = null;
    114         StringBuffer resultBuffer = new StringBuffer();
    115         String tempLine = null;
    116         
    117         if (httpURLConnection.getResponseCode() >= 300) {
    118             throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
    119         }
    120         
    121         try {
    122             inputStream = httpURLConnection.getInputStream();
    123             inputStreamReader = new InputStreamReader(inputStream);
    124             reader = new BufferedReader(inputStreamReader);
    125             
    126             while ((tempLine = reader.readLine()) != null) {
    127                 resultBuffer.append(tempLine);
    128             }
    129             
    130         } finally {
    131             
    132             if (reader != null) {
    133                 reader.close();
    134             }
    135             
    136             if (inputStreamReader != null) {
    137                 inputStreamReader.close();
    138             }
    139             
    140             if (inputStream != null) {
    141                 inputStream.close();
    142             }
    143             
    144         }
    145         
    146         return resultBuffer.toString();
    147     }
    148     
    149 }
  • 相关阅读:
    P2176 [USACO14FEB]路障Roadblock
    【最短路】Dijkstra+ 链式前向星+ 堆优化(优先队列)
    图论其一:图的存储
    【计算几何】二维凸包——Graham's Scan法
    P2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
    P2639 [USACO09OCT]Bessie的体重问题 【背包问题】
    如何评价代码质量的高低
    乔新亮-衡量企业 IT 团队价值的唯一指标是什么
    我总结了平台的5道坎
    hadoop namenode的工作机制
  • 原文地址:https://www.cnblogs.com/remember-forget/p/9429363.html
Copyright © 2011-2022 走看看