java中怎样用post、get、put请求
2016-06-02 11:35
330 查看
java中我们有时需要调用第三方的接口,但是第三方的接口都是http形式的,这是需要用post,get,put请求类型请求
1、GET方式请求
public static String httpGet(String getUrl,Map<String, String> getHeaders) throws IOException {
URL getURL = new URL(getUrl);
HttpURLConnection connection = (HttpURLConnection) getURL.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");//在get请求中这是能在各个浏览器兼容json
if(getHeaders != null) {
for(String pKey : getHeaders.keySet()) {
connection.setRequestProperty(pKey, getHeaders.get(pKey));
}
}
connection.connect();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sbStr = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sbStr.append(line);
}
bufferedReader.close();
connection.disconnect();
return new String(sbStr.toString().getBytes(),"utf-8");
}
2、POST方式请求
public static String httpPost(String postUrl,Map<String, String> postHeaders, String postEntity) throws IOException {
URL postURL = new URL(postUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) postURL.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setUseCaches(false);
httpURLConnection.setInstanceFollowRedirects(true);
//application/json x-www-form-urlencoded
//httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
StringBuilder sbStr = new StringBuilder();
if(postHeaders != null) {
for(String pKey : postHeaders.keySet()) {
httpURLConnection.setRequestProperty(pKey, postHeaders.get(pKey));
}
}
if(postEntity != null) {
JSONObject obj = new JSONObject();
obj.put("plate",postEntity);
PrintWriter out = new PrintWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(),"utf-8"));
out.println(obj);
out.close();
//httpURLConnection.getInputStream()
BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"utf-8")); //解决返回值汉字乱码的问题
String inputLine;
while ((inputLine = in.readLine()) != null) {
sbStr.append(inputLine);
}
in.close();
}
httpURLConnection.disconnect();
System.out.println("fsdfdf:"+sbStr.toString());
return new String(sbStr.toString().getBytes(),"utf-8");
}
4000
3、PUT请求
public static String HttpPut(String postUrl,Map<String, String> postHeaders,String postEntity) throws Exception {
URL postURL = new URL(postUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) postURL.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("PUT");
httpURLConnection.setUseCaches(false);
httpURLConnection.setInstanceFollowRedirects(true);
//application/json x-www-form-urlencoded
//httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//表单上传的模式
httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");//json格式上传的模式
StringBuilder sbStr = new StringBuilder();
if(postHeaders != null) {
for(String pKey : postHeaders.keySet()) {
httpURLConnection.setRequestProperty(pKey, postHeaders.get(pKey));
}
}
if(postEntity != null) {
JSONObject obj = new JSONObject();
obj.put("plate","京NB1314");
PrintWriter out = new PrintWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(),"utf-8"));
out.println(obj);
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection
.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
sbStr.append(inputLine);
}
in.close();
}
httpURLConnection.disconnect();
return new String(sbStr.toString().getBytes(),"utf-8");
}
1、GET方式请求
public static String httpGet(String getUrl,Map<String, String> getHeaders) throws IOException {
URL getURL = new URL(getUrl);
HttpURLConnection connection = (HttpURLConnection) getURL.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");//在get请求中这是能在各个浏览器兼容json
if(getHeaders != null) {
for(String pKey : getHeaders.keySet()) {
connection.setRequestProperty(pKey, getHeaders.get(pKey));
}
}
connection.connect();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sbStr = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sbStr.append(line);
}
bufferedReader.close();
connection.disconnect();
return new String(sbStr.toString().getBytes(),"utf-8");
}
2、POST方式请求
public static String httpPost(String postUrl,Map<String, String> postHeaders, String postEntity) throws IOException {
URL postURL = new URL(postUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) postURL.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setUseCaches(false);
httpURLConnection.setInstanceFollowRedirects(true);
//application/json x-www-form-urlencoded
//httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
StringBuilder sbStr = new StringBuilder();
if(postHeaders != null) {
for(String pKey : postHeaders.keySet()) {
httpURLConnection.setRequestProperty(pKey, postHeaders.get(pKey));
}
}
if(postEntity != null) {
JSONObject obj = new JSONObject();
obj.put("plate",postEntity);
PrintWriter out = new PrintWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(),"utf-8"));
out.println(obj);
out.close();
//httpURLConnection.getInputStream()
BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"utf-8")); //解决返回值汉字乱码的问题
String inputLine;
while ((inputLine = in.readLine()) != null) {
sbStr.append(inputLine);
}
in.close();
}
httpURLConnection.disconnect();
System.out.println("fsdfdf:"+sbStr.toString());
return new String(sbStr.toString().getBytes(),"utf-8");
}
4000
3、PUT请求
public static String HttpPut(String postUrl,Map<String, String> postHeaders,String postEntity) throws Exception {
URL postURL = new URL(postUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) postURL.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("PUT");
httpURLConnection.setUseCaches(false);
httpURLConnection.setInstanceFollowRedirects(true);
//application/json x-www-form-urlencoded
//httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//表单上传的模式
httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");//json格式上传的模式
StringBuilder sbStr = new StringBuilder();
if(postHeaders != null) {
for(String pKey : postHeaders.keySet()) {
httpURLConnection.setRequestProperty(pKey, postHeaders.get(pKey));
}
}
if(postEntity != null) {
JSONObject obj = new JSONObject();
obj.put("plate","京NB1314");
PrintWriter out = new PrintWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(),"utf-8"));
out.println(obj);
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection
.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
sbStr.append(inputLine);
}
in.close();
}
httpURLConnection.disconnect();
return new String(sbStr.toString().getBytes(),"utf-8");
}
相关文章推荐
- Java单元测试(Junit+Mock+代码覆盖率)
- java程序拥有的三个基本类加载器
- spring MVC 运行简单分析
- Java学习之LinkedHashMap
- spring和springMVC父子容器的原理
- Java基础资料
- 使用Validator做SpringMVC的验证框架 - 配置SpringMVC环境
- Java排错随笔
- Struts2---OGNL表达式和EL表达式
- (视频)Erich Gamma 与 Visual Studio Online 的一点野史
- eclipse中改变js文件编码方法
- 使用Springboot和Hystrix构建API Gateway
- java-将域名转换为ip地址
- springmvc笔记
- Android Studio中如何更改JDK和SDK的路径
- JAVA解析XML文件
- SpringMVC和MyBatis整合时
- java编码规范
- Java NIO系列教程(十一) Pipe
- Java并发-任务执行和取消关闭