java中怎样用post,get,put请求
答案:1 悬赏:0 手机版
解决时间 2021-03-27 22:54
- 提问者网友:孤山下
- 2021-03-27 14:06
java中怎样用post,get,put请求
最佳答案
- 五星知识达人网友:十年萤火照君眠
- 2021-03-27 14:35
java中用post,get,put请求方法:
public static String javaHttpGet(String url,String charSet){
String resultData = null;
try {
URL pathUrl = new URL(url); //创建一个URL对象
HttpURLConnection urlConnect = (HttpURLConnection) pathUrl.openConnection(); //打开一个HttpURLConnection连接
urlConnect.setConnectTimeout(30000); // 设置连接超时时间
urlConnect.connect();
if (urlConnect.getResponseCode() == 200) { //请求成功
resultData = readInputStream(urlConnect.getInputStream(), charSet);
}
} catch (MalformedURLException e) {
LogL.getInstance().getLog().error("URL出错!", e);
} catch (IOException e) {
LogL.getInstance().getLog().error("读取数据流出错!", e);
}
return resultData;
}
public static String javaHttpPost(String url,Map map,String charSet){
String resultData=null;
StringBuffer params = new StringBuffer();
try {
Iterator> ir = map.entrySet().iterator();
while (ir.hasNext()) {
Map.Entry entry = (Map.Entry) ir.next();
params.append(URLEncoder.encode(entry.getKey(),charSet) + "=" + URLEncoder.encode(entry.getValue().toString(), charSet) + "&");
}
byte[] postData = params.deleteCharAt(params.length()).toString().getBytes();
URL pathUrl = new URL(url); //创建一个URL对象
HttpURLConnection urlConnect = (HttpURLConnection) pathUrl.openConnection();
urlConnect.setConnectTimeout(30000); // 设置连接超时时间
urlConnect.setDoOutput(true); //post请求必须设置允许输出
urlConnect.setUseCaches(false); //post请求不能使用缓存
urlConnect.setRequestMethod("POST"); //设置post方式请求
urlConnect.setInstanceFollowRedirects(true);
urlConnect.setRequestProperty("Content-Type","application/x-www-form-urlencoded; charset="+charSet);// 配置请求Content-Type
urlConnect.connect(); // 开始连接
DataOutputStream dos = new DataOutputStream(urlConnect.getOutputStream()); // 发送请求参数
dos.write(postData);
dos.flush();
dos.close();
if (urlConnect.getResponseCode() == 200) { //请求成功
resultData = readInputStream(urlConnect.getInputStream(),charSet);
}
} catch (MalformedURLException e) {
LogL.getInstance().getLog().error("URL出错!", e);
} catch (IOException e) {
LogL.getInstance().getLog().error("读取数据流出错!", e);
} catch (Exception e) {
LogL.getInstance().getLog().error("POST出错!", e);
}
return resultData;
}
public static String javaHttpGet(String url,String charSet){
String resultData = null;
try {
URL pathUrl = new URL(url); //创建一个URL对象
HttpURLConnection urlConnect = (HttpURLConnection) pathUrl.openConnection(); //打开一个HttpURLConnection连接
urlConnect.setConnectTimeout(30000); // 设置连接超时时间
urlConnect.connect();
if (urlConnect.getResponseCode() == 200) { //请求成功
resultData = readInputStream(urlConnect.getInputStream(), charSet);
}
} catch (MalformedURLException e) {
LogL.getInstance().getLog().error("URL出错!", e);
} catch (IOException e) {
LogL.getInstance().getLog().error("读取数据流出错!", e);
}
return resultData;
}
public static String javaHttpPost(String url,Map
String resultData=null;
StringBuffer params = new StringBuffer();
try {
Iterator
while (ir.hasNext()) {
Map.Entry
params.append(URLEncoder.encode(entry.getKey(),charSet) + "=" + URLEncoder.encode(entry.getValue().toString(), charSet) + "&");
}
byte[] postData = params.deleteCharAt(params.length()).toString().getBytes();
URL pathUrl = new URL(url); //创建一个URL对象
HttpURLConnection urlConnect = (HttpURLConnection) pathUrl.openConnection();
urlConnect.setConnectTimeout(30000); // 设置连接超时时间
urlConnect.setDoOutput(true); //post请求必须设置允许输出
urlConnect.setUseCaches(false); //post请求不能使用缓存
urlConnect.setRequestMethod("POST"); //设置post方式请求
urlConnect.setInstanceFollowRedirects(true);
urlConnect.setRequestProperty("Content-Type","application/x-www-form-urlencoded; charset="+charSet);// 配置请求Content-Type
urlConnect.connect(); // 开始连接
DataOutputStream dos = new DataOutputStream(urlConnect.getOutputStream()); // 发送请求参数
dos.write(postData);
dos.flush();
dos.close();
if (urlConnect.getResponseCode() == 200) { //请求成功
resultData = readInputStream(urlConnect.getInputStream(),charSet);
}
} catch (MalformedURLException e) {
LogL.getInstance().getLog().error("URL出错!", e);
} catch (IOException e) {
LogL.getInstance().getLog().error("读取数据流出错!", e);
} catch (Exception e) {
LogL.getInstance().getLog().error("POST出错!", e);
}
return resultData;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯