java發(fā)送get請(qǐng)求和post請(qǐng)求示例
java向服務(wù)端發(fā)送GET和POST請(qǐng)求
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請(qǐng)求
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("請(qǐng)求出現(xiàn)了問(wèn)題!");
}
} catch (IOException e) {
e.printStackTrace();
}finally{
in.close();
httpConn.disconnect();
}
return null;
}
//發(fā)送一個(gè)GET請(qǐng)求,參數(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請(qǐng)求參數(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("請(qǐng)求出現(xiàn)了問(wè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);
}
}
相關(guān)文章
SpringBoot?項(xiàng)目打成?jar后加載外部配置文件的操作方法
這篇文章主要介紹了SpringBoot?項(xiàng)目打成?jar后加載外部配置文件的操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
解決@Value注解不能注入static修飾的屬性問(wèn)題
這篇文章主要介紹了解決@Value注解不能注入static修飾的屬性問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
利用Java對(duì)PDF文件進(jìn)行電子簽章的實(shí)戰(zhàn)過(guò)程
隨著電子賬單、回單、通知、合同的流行,電子文檔的可信度變得非常重要,為防止非法篡改,確保文檔的權(quán)威性,我們可以對(duì)PDF進(jìn)行電子簽章,這篇文章主要給大家介紹了關(guān)于如何利用Java對(duì)PDF文件進(jìn)行電子簽章的相關(guān)資料,需要的朋友可以參考下2021-07-07
SpringBoot項(xiàng)目導(dǎo)入外部jar包的詳細(xì)指南
在開(kāi)發(fā)SpringBoot項(xiàng)目時(shí),我們經(jīng)常需要引入一些外部的jar包來(lái)增強(qiáng)項(xiàng)目的功能,這些jar包可能不是Maven中央倉(cāng)庫(kù)中的,或者我們想要使用特定版本的jar包,本文將詳細(xì)介紹如何在SpringBoot項(xiàng)目中導(dǎo)入外部jar包,需要的朋友可以參考下2024-10-10

