java发送http的get和post请求
import java.io.*;
import java.net.URL;
import java.util.Map;
import java.net.HttpURLConnection;; public class HttpRequest{ public static String sendGet(String url) {
String result = "";
BufferedReader in = null;
try {
URL realurl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) realurl.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch(Exception e) {
System.out.println("发送get请求失败:"+e);
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
} public static String sendPost(String url, String params) {
String encoding = "UTF-8";
String result = "";
BufferedReader in = null;
try {
byte[] data = params.getBytes(encoding);
URL base_url = new URL(url);
HttpURLConnection conn = (HttpURLConnection) base_url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
System.out.println(conn.getResponseCode());
System.out.println(conn.getResponseMessage());
if (conn.getResponseCode() == 200) {
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 {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
}
java发送http的get和post请求的更多相关文章
- java发送http的get、post请求
转载博客:http://www.cnblogs.com/zhuawang/archive/2012/12/08/2809380.html Http请求类 package wzh.Http; impor ...
- java发送http的get、post请求[转]
原文链接:http://www.cnblogs.com/zhuawang/archive/2012/12/08/2809380.html package wzh.Http; import java.i ...
- java发送http的get、post请求【备忘】
类 package com.dsideal.kq.Controller; import java.io.BufferedReader; import java.io.IOException; impo ...
- [转java发送http的get、post请求]
Http请求类 package wzh.Http; import java.io.BufferedReader; import java.io.IOException; import java.io. ...
- 用JAVA发送一个XML格式的HTTP请求
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStr ...
- [zhuan]java发送http的get、post请求
http://www.cnblogs.com/zhuawang/archive/2012/12/08/2809380.html Http请求类 package wzh.Http; import jav ...
- java发送application/json格式的post请求,需要登陆
package util; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWri ...
- Java发送Http请求并获取状态码
通过Java发送url请求,查看该url是否有效,这时我们可以通过获取状态码来判断. try { URL u = new URL("http://10.1.2.8:8080/fqz/page ...
- 编写爬虫(spider)的预备知识:用java发送HTTP请求
使用原生API来发送http请求,而不是使用apache的库,原因在于这个第三方库变化实在太快了,每个版本都有不小的变化.对于程序员来说,使用它反而会有很多麻烦,比如自己曾经写过的代码将无法复用. 原 ...
随机推荐
- redis之(十七)自己实现redis的cluster集群环境的搭建
[一]创建不同节点的配置文件和目录.并将配置文件中的port,cluster-enable,daemonize项做修改. --->port:修改成redis实例对应的端口号 --->clu ...
- Populating Next Right Pointers in Each Node I&&II ——II仍然需要认真看看
Populating Next Right Pointers in Each Node I Given a binary tree struct TreeLinkNode { TreeLinkNode ...
- [转载]Python命令行参数学习
转载自: http://blog.163.com/weak_time/blog/static/25852809120169333247925/ Python的命令行参数,提供了很多有用的功能,可以方便 ...
- AC日记——矩阵取数游戏 洛谷 P1005
矩阵取数游戏 思路: dp+高精: 代码: #include <bits/stdc++.h> using namespace std; #define ll long long struc ...
- React Native - 5 ListView实现图文混排
首先在根目录下建一个images文件夹,准备好图片 准备datasource 准备图片资源 准备renderRow方法 记得要import相应的类,ListView, Image, Touchable ...
- CentOS7.5下gnome-terminal+vim的solarized配色方案
1.简介 Solarized是一款包括浅色和深色的配色方案,适用于很多应用,可以让你的应用看起来更加漂亮!官网地址:http://ethanschoonover.com/solarized 2.设置g ...
- CentOS7.5字体美化
背景知识 有衬线 (Serif) 无衬线 (Sans Serif) 和等宽 (Monospace) 字型 1 有衬线 (Serif) 字型是比较正式的字体,比划粗细不一,在笔划的边缘有装饰部分(我的理 ...
- 排序算法总结(C语言版)
排序算法总结(C语言版) 1. 插入排序 1.1 直接插入排序 1.2 Shell排序 2. 交换排序 2.1 冒泡排序 2.2 快速排序 3. 选择 ...
- Eclipse IDE下的Spring框架使用简单实例
Eclipse IDE下的Spring框架使用简单实例 1 准备Java jdk安装. Eclipse软件安装.根据系统安装32/64版本,选择Eclipse IDE for Java Develop ...
- qTip2 Events详细说明
绑定事件: 这个API触发一些特殊的事件(以下详细信息),允许你给qTip分配多个时间监听,和为某一事件做出响应,例如: 我们绑定一个事件句柄,它将侦听qTip的移动的事件,和更新DIV元素里面显示的 ...