请通过浏览器功能收藏网页

java 发送POST请求携带参数的GET模式 POST模式发送接收数据 语言问题

发布时间:2018-08-08 09:48:41  作者:本站编辑  来源:本站原创  浏览次数:
我有话说 | 分享 |
www.javainfo.com.cn 上干货 欢迎收藏


在编写与服务交互的项目时候 经常会通过HTTP协议与服务器进行交互,本实例用java编写了一些通用的HTTP协议发送数据 接收数据的标准函数


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.HttpURLConnection;

import java.net.URL;


public class UriPost {


// 发送一个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());

System.out.println(params);

out.println(params);

out.flush();

System.out.println(httpConn.getResponseCode());

// 读取响应

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 {

System.out.println("sss");

throw new Exception("请求出现了问题!");

}

} catch (IOException e) {

e.printStackTrace();

} finally {

in.close();

out.close();

httpConn.disconnect();

}

return null;

}


public static String post2(String path, String params) {

HttpURLConnection httpConn = null;

BufferedReader in = null;

PrintWriter out = null;

StringBuffer content = new StringBuffer();

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());

System.out.println(params);

out.println(params);

out.flush();

System.out.println(httpConn.getResponseCode());

String tempStr = "";

in = new BufferedReader(new InputStreamReader(

httpConn.getInputStream()));

while ((tempStr = in.readLine()) != null) {

content.append(tempStr);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

out.close();

httpConn.disconnect();

}

return content.toString();

}



public  static String post3 (String action, String queryString){

StringBuffer content = new StringBuffer();

BufferedReader Br = null;

try {

URL url = new URL(action);

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

http.setRequestMethod("POST");

//http.setConnectTimeout(0);

http.setInstanceFollowRedirects(true);

http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

// http.setRequestProperty("Content-Length", "1000");

// http.setDefaultUseCaches(false);

http.setDoOutput(true);


PrintWriter out = new PrintWriter(http.getOutputStream());

out.print(queryString);//传入参数

out.close();

http.connect();//连接


//返回流

InputStream in = http.getInputStream();

String tempStr = "";

Br = new BufferedReader(new InputStreamReader(in));

while ((tempStr = Br.readLine()) != null) {

content.append(tempStr);

}

} catch (IOException e) {

e.printStackTrace();

}

return content.toString();

  }

}


如有疑问 请留言 欢迎提供建议

评论已有 0