Http Post请求
来源:互联网 发布:网络教育统考 编辑:程序博客网 时间:2024/06/14 21:04
一般的POST请求
/**
* 向指定 URL 发送POST方法的请求
*
* @param url
* 发送请求的 URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(200);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
流的POST请求
// 功能: postBody形式发送数据
// @param urlPath 对方地址
// @param json 要传送的数据
// @return
// @throws Exception
public static String postBody(String urlPath, String json) throws Exception {
try {
// Configure and open a connection to the site you will send the
// request
URL url = new URL(urlPath);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
// 设置doOutput属性为true表示将使用此urlConnection写入数据
urlConnection.setDoOutput(true);
// 定义待写入数据的内容类型,我们设置为application/x-www-form-urlencoded类型
urlConnection.setRequestProperty("content-type",
"application/x-www-form-urlencoded");
// 得到请求的输出流对象
// OutputStreamWriter out = new OutputStreamWriter(
// urlConnection.getOutputStream());
OutputStreamWriter out = new OutputStreamWriter(
urlConnection.getOutputStream(),"UTF-8");
// 把数据写入请求的Body
out.write(json);
out.flush();
out.close();
// 从服务器读取响应
InputStream inputStream = urlConnection.getInputStream();
String encoding = urlConnection.getContentEncoding();
String body = IOUtils.toString(inputStream, encoding);
inputStream.close();
return body;
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
阅读全文
2 0
- android http post请求
- http的post请求
- C# http post请求
- HTTP 模拟post请求
- http post get 请求
- http请求post
- ios HTTP post请求
- HTTP GET/POST 请求
- http post get请求
- http post 客户端请求
- HTTP POST 网络请求
- http 原生post 请求
- http post请求
- http请求 get post
- http post请求
- http post 请求
- http post get请求
- http post请求
- 函数设计
- Funny Car Racing UVA
- Java-对list里面的元素进行冒泡排序
- HDU 3988 Harry Potter and the Hide Story
- 腾讯云小直播demo配置流程
- Http Post请求
- ios-事件浅析
- table类型(字典)两种方法调用里面的对象
- [干货]Android最全面试总结——这些Android面试题你一定需要
- LeetCode-- Search Insert Position
- NYOJ1111游戏人生(区间DP)
- POJ2503 UVA10282 Babelfish
- JavaDay03流程控制
- 构造式