java原生http请求post&get
来源:互联网 发布:多线程编程demojava 编辑:程序博客网 时间:2024/06/08 03:33
Java原生http请求post,需要的朋友可以参考下。
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class HttpUtils {
public static String sendPost(String uri, String charset) {
String result = null;
InputStream in = null;
try {
URL url = new URL(uri);
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
urlcon.setRequestMethod("POST");
urlcon.connect();// 获取连接
in = urlcon.getInputStream();
BufferedReader buffer = new BufferedReader(new InputStreamReader(
in, charset));
StringBuffer bs = new StringBuffer();
String line = null;
while ((line = buffer.readLine()) != null) {
bs.append(line);
}
result = bs.toString();
} catch (Exception e) {
System.out.println("[请求异常][地址:" + uri + "][错误信息:" + e.getMessage()
+ "]");
} finally {
try {
if (null != in)
in.close();
} catch (Exception e2) {
System.out.println("[关闭流异常][错误信息:" + e2.getMessage() + "]");
}
}
return result;
}
/**
*
* @param uri
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @param charset
* @return
*/
public static String sendPost(String uri, String param, String charset) {
String result = null;
PrintWriter out = null;
InputStream in = null;
try {
URL url = new URL(uri);
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
urlcon.setDoInput(true);
urlcon.setDoOutput(true);
urlcon.setUseCaches(false);
urlcon.setRequestMethod("POST");
urlcon.connect();// 获取连接
out = new PrintWriter(urlcon.getOutputStream());
out.print(param);
out.flush();
in = urlcon.getInputStream();
BufferedReader buffer = new BufferedReader(new InputStreamReader(
in, charset));
StringBuffer bs = new StringBuffer();
String line = null;
while ((line = buffer.readLine()) != null) {
bs.append(line);
}
result = bs.toString();
} catch (Exception e) {
System.out.println("[请求异常][地址:" + uri + "][参数:" + param + "][错误信息:"
+ e.getMessage() + "]");
} finally {
try {
if (null != in)
in.close();
if (null != out)
out.close();
} catch (Exception e2) {
System.out.println("[关闭流异常][错误信息:" + e2.getMessage() + "]");
}
}
return result;
}
/**
* Do GET request
* @param url
* @return
* @throws Exception
* @throws IOException
*/
public String doGet(String url) throws Exception {
URL localURL = new URL(url);
URLConnection connection = localURL.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
StringBuffer resultBuffer = new StringBuffer();
String tempLine = null;
//响应失败
if (httpURLConnection.getResponseCode() >= 300) {
throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
}
try {
inputStream = httpURLConnection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream);
reader = new BufferedReader(inputStreamReader);
while ((tempLine = reader.readLine()) != null) {
resultBuffer.append(tempLine);
}
} finally {
if (reader != null) {
reader.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (inputStream != null) {
inputStream.close();
}
}
return resultBuffer.toString();
}
}
阅读全文
0 0
- java原生http请求post&get
- java原生http请求post
- iOS原生Http请求,get post 异步
- JAVA 发送Http请求,GET、POST请求
- http 原生post 请求
- 【Go】原生http get和post请求框架
- java发送HTTP GET POST请求
- java发送get、post http请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java发送http的get、post请求
- java 发送http请求post get
- java http 请求get 和post
- java发送http的get、post请求
- java发送http的get、post请求
- Callable和Future
- sql练习2
- String 字符串 文件流
- 信息收集
- ETL抽取工具informatica powercenter designer实例(一)
- java原生http请求post&get
- JVM内存模型详解
- traits编程技术
- Struts2之Action类概要
- thinkphp3.2 更新数据表某一字段的全部内容
- ireport导出pdf(二)
- Error:(15) No resource identifier found for attribute 'roundIcon' in package 'android'
- ios-autolayout添加约束的规则
- LINQ