一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - JAVA教程 - java發送get請求和post請求示例

java發送get請求和post請求示例

2019-11-15 14:28java教程網 JAVA教程

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

java向服務端發送GET和POST請求

 

復制代碼代碼如下:


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 {
 //發送一個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();

   //讀取響應
   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("請求出現了問題!");
   }
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   in.close();
   httpConn.disconnect();
  }
  return null;
 }
 //發送一個GET請求,參數形式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);

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

   //讀取響應
   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("請求出現了問題!");
   }
  } 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);
 }

}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲香蕉视频 | 成人免费在线视频 | 国产精品久久久久久 | 男女18一级大黄毛片免 | 美女靠逼免费网站 | 青青热久免费精品视频网站 | 欧美人妖大啪啪 | 久久精品动漫99精品动漫 | 精品淑女少妇AV久久免费 | 99热久久这里只有精品6国产网 | 好吊色青青青国产综合在线观看 | 色老板成人永久免费视频 | 国产精品亚洲综合第一区 | 精品亚洲麻豆1区2区3区 | 国产精品久久毛片完整版 | 欧美一区不卡二区不卡三区 | 男人边吃奶边做好爽视频免费 | 99久久伊人精品波多野结衣 | 午夜国产在线观看 | 日本一区二区三区国产 | 免费观看大片毛片 | 亚洲天堂精品在线 | 国产日产欧产精品精品软件 | 国产一级片在线 | 茄子香蕉视频 | 色综合91久久精品中文字幕 | 国产第一福利视频导航在线 | 免费标准高清看机机桶机机 | 高清国产欧美一v精品 | 果冻传媒第一二三专区 | 91免费高清视频 | 国产亚洲女在线线精品 | 236zz宅宅最新伦理 | 久久久这里有精品999 | 星空无限传媒xk8046 | 韩国理论片最新第一页 | 亚洲精品第五页中文字幕 | 国产91青青成人a在线 | 久久精品国产色蜜蜜麻豆国语版 | 久久精品观看影院2828 | 亚洲国产综合久久精品 |