请求一个url 获得返回结果(doPost doGet方式)
来源:互联网 发布:mac能装千牛吗 编辑:程序博客网 时间:2024/06/06 02:06
public static String post(String urlStr, Map<String, String> map) {
if (urlStr == null || map == null || map.size() == 0) {
return null;
}
try {
StringBuffer urt = new StringBuffer();
urt.append(urlStr);
urt.append("?");
boolean add = true;
for (Entry e : map.entrySet()) {
if (add) {
add = false;
} else {
urt.append("&");
}
urt.append(e.getKey());
urt.append("=");
urt.append(e.getValue() == null ? "" : e.getValue());
}
String responseContent = null;
HttpURLConnection url_con = null;
URL url = new URL(urt.toString());
url_con = (HttpURLConnection) url.openConnection();
url_con.setRequestMethod("POST");
url_con.setDoOutput(true);
StringBuffer params = new StringBuffer();
byte[] b = params.toString().getBytes();
url_con.getOutputStream().write(b, 0, b.length);
url_con.getOutputStream().flush();
url_con.getOutputStream().close();
url_con.setReadTimeout(1500);
InputStream in = url_con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String tempLine = rd.readLine();
StringBuffer tempStr = new StringBuffer();
while (tempLine != null) {
tempStr.append(tempLine);
tempLine = rd.readLine();
}
responseContent = tempStr.toString();
rd.close();
in.close();
// System.out.println(tempStr.toString());
return tempStr.toString();
} catch (Exception e1) {
return null;
}
}
if (urlStr == null || map == null || map.size() == 0) {
return null;
}
try {
StringBuffer urt = new StringBuffer();
urt.append(urlStr);
urt.append("?");
boolean add = true;
for (Entry e : map.entrySet()) {
if (add) {
add = false;
} else {
urt.append("&");
}
urt.append(e.getKey());
urt.append("=");
urt.append(e.getValue() == null ? "" : e.getValue());
}
String responseContent = null;
HttpURLConnection url_con = null;
URL url = new URL(urt.toString());
url_con = (HttpURLConnection) url.openConnection();
url_con.setRequestMethod("POST");
url_con.setDoOutput(true);
StringBuffer params = new StringBuffer();
byte[] b = params.toString().getBytes();
url_con.getOutputStream().write(b, 0, b.length);
url_con.getOutputStream().flush();
url_con.getOutputStream().close();
url_con.setReadTimeout(1500);
InputStream in = url_con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String tempLine = rd.readLine();
StringBuffer tempStr = new StringBuffer();
while (tempLine != null) {
tempStr.append(tempLine);
tempLine = rd.readLine();
}
responseContent = tempStr.toString();
rd.close();
in.close();
// System.out.println(tempStr.toString());
return tempStr.toString();
} catch (Exception e1) {
return null;
}
}
- 请求一个url 获得返回结果(doPost doGet方式)
- HttpClient doPost doGet 请求 返回内存流
- android doGet和doPost请求
- httpclient的dopost和doget请求
- 小白胡说:Servlet中doGet和doPost两种请求方式的区别
- doGet doPost
- doGet,doPost
- Java读取Url请求获得返回数据
- (java)自己模拟http请求的dopost和doget
- JavaWeb-3 Servlet之doGet doPost请求&生命周期简述
- Servlet--HttpServlet实现doGet和doPost请求的原理
- okHttp拦截器放在doGet和doPost请求中
- http通信doGet与doPost方式小例子
- servlet doGet doPost区别
- doGet()与doPost
- Servlet---doGet(), doPost
- 浅谈doGet和doPost
- Servlet doget dopost 区别
- 手把手教你如何实现Binder的客户端程序(图文)
- 描述程序员牛人:牛人为什么牛?
- WPF中多个StoryBoard动画切换控制
- 天气预报API
- 我脑残实验室Verilog学习笔记——在Xilinx ISE中运用符号设计顶层模块的方法
- 请求一个url 获得返回结果(doPost doGet方式)
- Oracle定时调用存储过程(简单易懂)
- sha-2加密
- C51---全部寄存器---
- 如何成为“10倍效率”开发者
- 直方图均衡法
- iPhone文件系统NSFileManager讲解
- C++中关于指向对象成员函数打指针例子
- 2012年CISSP考试时间公告(源自ISC2)