java http请求 demo_http普通请求demo

packagecn.mr.li;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.net.HttpURLConnection;importjava.net.URL;public classHttpTest {public staticString request(String strUrl,String requestType) {//https://blog.csdn.net/longshehe9319/article/details/80509829

HttpURLConnection connection;//声明连接,初始值为null;

BufferedReader buffer ;//声明缓存流

StringBuffer resultBuffer;try{//新建一个新的url

URL url = newURL(strUrl);//打开连接

connection =(HttpURLConnection)url.openConnection();//哪种请求方式 POST GET....

connection.setRequestMethod(requestType);//设置请求需要返回的数据类型和字符集类型

connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");//允许写出

connection.setDoOutput(true);//允许读入

connection.setDoInput(true);//不使用缓存

connection.setUseCaches(false);//得到响应码

int responseCode =connection.getResponseCode();if(responseCode ==HttpURLConnection.HTTP_OK){//得到响应流

InputStream inputStream =connection.getInputStream();//将响应流转换成字符串

resultBuffer = newStringBuffer();

String line;

buffer= new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));while ((line = buffer.readLine()) != null) {

resultBuffer.append(line);

}

buffer.close();returnresultBuffer.toString();

}

}catch(IOException e) {

e.printStackTrace();

}return "";

}public static void main(String[] args) throwsInterruptedException {//String result = request("http://139.9.35.73:21200/chat/get/online?id=123", "POST");//System.out.println(result);//post();

System.out.println(4 ^ 2);

System.out.println(3 ^ 2);

System.out.println(3 ^ 3);

System.out.println(9 ^ 3);

}/*** 可以传body值的post请求*/

public static voidpost() {try{

URL url= new URL("http://139.9.35.73:21200/chat/get/online");

HttpURLConnection connection=(HttpURLConnection) url.openConnection();

connection.setRequestMethod("POST");

connection.setDoOutput(true);

connection.setDoInput(true);

connection.setUseCaches(false);

connection.connect();

String body= "id=123456";

BufferedWriter writer= new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));

writer.write(body);

writer.close();int responseCode =connection.getResponseCode();if(responseCode ==HttpURLConnection.HTTP_OK){

InputStream inputStream=connection.getInputStream();

StringBuffer resultBuffer= newStringBuffer();

String line;

BufferedReader buffer= new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));while ((line = buffer.readLine()) != null) {

resultBuffer.append(line);

}

System.out.println("kwwl"+"result============="+resultBuffer.toString());

}

}catch(Exception e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值