激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - java發(fā)送get請求和post請求示例

java發(fā)送get請求和post請求示例

2019-11-15 14:28java教程網(wǎng) JAVA教程

這篇文章主要介紹了java發(fā)送get請求和post請求示例,需要的朋友可以參考下

java向服務(wù)端發(fā)送GET和POST請求

 

復(fù)制代碼代碼如下:


package com.hongyuan.test;

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpClient {
 //發(fā)送一個(gè)GET請求
 public static String get(String path) throws Exception{
  HttpURLConnection httpConn=null;
  BufferedReader in=null;
  try {
   URL url=new URL(path);
   httpConn=(HttpURLConnection)url.openConnection();

   //讀取響應(yīng)
   if(httpConn.getResponseCode()==HttpURLConnection.HTTP_OK){
    StringBuffer content=new StringBuffer();
    String tempStr="";
    in=new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
    while((tempStr=in.readLine())!=null){
     content.append(tempStr);
    }
    return content.toString();
   }else{
    throw new Exception("請求出現(xiàn)了問題!");
   }
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   in.close();
   httpConn.disconnect();
  }
  return null;
 }
 //發(fā)送一個(gè)GET請求,參數(shù)形式key1=value1&key2=value2...
 public static String post(String path,String params) throws Exception{
  HttpURLConnection httpConn=null;
  BufferedReader in=null;
  PrintWriter out=null;
  try {
   URL url=new URL(path);
   httpConn=(HttpURLConnection)url.openConnection();
   httpConn.setRequestMethod("POST");
   httpConn.setDoInput(true);
   httpConn.setDoOutput(true);

   //發(fā)送post請求參數(shù)
   out=new PrintWriter(httpConn.getOutputStream());
   out.println(params);
   out.flush();

   //讀取響應(yīng)
   if(httpConn.getResponseCode()==HttpURLConnection.HTTP_OK){
    StringBuffer content=new StringBuffer();
    String tempStr="";
    in=new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
    while((tempStr=in.readLine())!=null){
     content.append(tempStr);
    }
    return content.toString();
   }else{
    throw new Exception("請求出現(xiàn)了問題!");
   }
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   in.close();
   out.close();
   httpConn.disconnect();
  }
  return null;
 }

 public static void main(String[] args) throws Exception {
  //String resMessage=HttpClient.get("http://localhost:3000/hello?hello=hello get");
  String resMessage=HttpClient.post("http://localhost:3000/hello", "hello=hello post");
  System.out.println(resMessage);
 }

}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 中文字幕极速在线观看 | 国产一级淫片免费看 | 91九色视频在线播放 | 永久久久 | 国产精品99免费视频 | 欧美成人精品一区二区男人小说 | 欧美不卡三区 | 免费看成年人视频在线 | 日日狠狠久久偷偷四色综合免费 | 久草在线资源观看 | 久久在草| 精品亚洲午夜久久久久91 | 日韩在线观看免费 | 91在线免费观看 | 日本aaa一级片| 91一区二区三区久久久久国产乱 | 欧美成人午夜精品久久久 | 亚洲成人国产 | 草草久久久| 黄色片免费在线 | 伊人手机在线观看 | 国产午夜精品视频免费不卡69堂 | 视频一区二区在线观看 | 国产精品呻吟 | 亚洲情av | 99re热精品视频 | 日韩欧美电影一区二区三区 | 久综合| 日本在线一区二区 | 色悠悠久久久久 | 香蕉久久久久久 | 欧美999| 一区二区三区无码高清视频 | 久久99久久98精品免观看软件 | 天天干导航| 久久久久久免费 | 99riav国产在线观看 | 毛片三区 | 国产精品午夜未成人免费观看 | 久久久久久久久久亚洲精品 | 91视频完整版 |