java发送http get请求,有两种方式。

第一种用URLConnection:

 public static String get(String url) throws IOException {
BufferedReader in = null; URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection connection = realUrl.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)");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
// 建立实际的连接
connection.connect();
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = in.readLine()) != null) {
sb.append(line);
} in.close();
return sb.toString();
}

第二种用java HTTP客户端:HttpGet、HttpClient、HttpResponse等

public static String httpGet(String url, String charset)
throws HttpException, IOException {
String json = null;
HttpGet httpGet = new HttpGet();
// 设置参数
try {
httpGet.setURI(new URI(url));
} catch (URISyntaxException e) {
throw new HttpException("请求url格式错误。"+e.getMessage());
}
// 发送请求
HttpClient client=HttpClients.createDefault();
HttpResponse httpResponse = client.execute(httpGet);
// 获取返回的数据
HttpEntity entity = httpResponse.getEntity();
byte[] body = EntityUtils.toByteArray(entity);
StatusLine sL = httpResponse.getStatusLine();
int statusCode = sL.getStatusCode();
if (statusCode == 200) {
json = new String(body, charset);
entity.consumeContent();
} else {
throw new HttpException("statusCode="+statusCode);
}
return json;
}

POST请求同理,这里只列出URLConnection方式:

public static String post(String url, String param, Map<String, String> header) throws IOException {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
//设置超时时间
conn.setConnectTimeout(5000);
conn.setReadTimeout(15000);
// 设置通用的请求属性
if (header != null) {
for (Map.Entry<String, String> entry : header.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
}
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);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(), "utf8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
return result;
}

代理

如果使用代理的话可以按如下编写一个设置代理的函数configProxy,然后在发送请求前调用一下这个函数就行了:

package com.tpot.DataDownload;

import java.util.Properties;

public class Configer {
public static void configProxy(){
Properties prop=System.getProperties(); prop.setProperty("proxySet","true");
prop.setProperty("http.proxyHost","proxy.xxxx.com");
prop.setProperty("http.proxyPort","8080");
prop.setProperty("http.proxyUser","xxxxx");
prop.setProperty("http.proxyPassword","xxxxx"); prop.setProperty("https.proxyHost","proxy.xxxxx.com");
prop.setProperty("https.proxyPort","8080");
prop.setProperty("https.proxyUser","xxxxx");
prop.setProperty("https.proxyPassword","xxxxx");
}
}

JAVA发送http GET/POST请求的两种方式+JAVA http 请求手动配置代理的更多相关文章

  1. C#中Post请求的两种方式发送参数链和Body的

    POST请求 有两种方式 一种是组装key=value这种参数对的方式 一种是直接把一个字符串发送过去 作为body的方式 我们在postman中可以看到 sfdsafd sdfsdfds publi ...

  2. 比较两种方式的form请求提交

    [一]浏览器form表单提交 表单提交, 适用于浏览器提交.像常见的pc端的网银支付,用户在商户商城购买商品,支付时商家系统把交易数据通过form表单提交到三方支付网关,然后用户在三方网关页面完成支付 ...

  3. 第二节:SSL证书的申请、配置(IIS通用)及跳转Https请求的两种方式

    一. 相关概念介绍 1. SSL证书服务 SSL证书服务由"服务商"联合多家国内外数字证书管理和颁发的权威机构.在xx云平台上直接提供的服务器数字证书.您可以在阿里云.腾讯云等平台 ...

  4. 解决 SharePoint 2010 拒绝访问爬网内容源错误的小技巧(禁用环回请求的两种方式)

    这里有一条解决在SharePoint 2010搜索爬网时遇到的“拒绝访问错误”的小技巧. 首先要检查默认内容访问帐户是否具有相应的访问权限,或者添加一条相应的爬网规则.如果目标资源库是一个ShareP ...

  5. 探讨Netty获取并检查Websocket握手请求的两种方式

    在使用Netty开发Websocket服务时,通常需要解析来自客户端请求的URL.Headers等等相关内容,并做相关检查或处理.本文将讨论两种实现方法. 方法一:基于HandshakeComplet ...

  6. java中读取配置文件ResourceBundle和Properties两种方式比较

    今天在开发的时候,需要把一些信息放到配置文件中,方便后续的修改,注意到用的是ResourceBundle读取配置文件的方式,记得之前也见过使用Properties的方式,就比较好奇这两种方式的区别,网 ...

  7. spring boot @ResponseBody转换JSON 时 Date 类型处理方法,Jackson和FastJson两种方式,springboot 2.0.9配置fastjson不生效官方解决办法

    spring boot @ResponseBody转换JSON 时 Date 类型处理方法 ,这里一共有两种不同解析方式(Jackson和FastJson两种方式,springboot我用的1.x的版 ...

  8. java发送http get请求的两种方式

    长话短说,废话不说 一.第一种方式,通过HttpClient方式,代码如下: public static String httpGet(String url, String charset) thro ...

  9. 安卓中使用OkHttp发送数据请求的两种方式(同、异步的GET、POST) 示例-- Android基础

    1.首先看一下最终效果的截图,看看是不是你想要的,这个年代大家都很忙,开门见山很重要! 简要说下,点击不同按钮可以实现通过不同的方式发送OkHttp请求,并返回数据,这里请求的是网页,所以返回的都是些 ...

随机推荐

  1. [svc]几种访问google方案

    最近老被人问起,有什么访问谷歌的方法可以推荐. 针对小白用户(使用sass式即可) iass sass pass区别 小白可以用(无需安装软件,些许收费):googlegae: https://m.2 ...

  2. WEB前端的性能优化

    转自:http://www.2cto.com/kf/201604/498725.html 网站的划分一般为二:前端和后台.我们可以理解成后台是用来实现网站的功能的,比如:实现用户注册,用户能够为文章发 ...

  3. JQuery EasyUI 请求/加载 两次

    解决方案如下: Html页面中的Table标签中包含class属性(class="easyui-datagrid"),删除即可.

  4. dbutils使用---QueryRunner实现in批量查询

    sql.append("AND a.").append(MchStore.STORE_PROVINCE_COL).append(" IN ("); for (i ...

  5. JQuery 中的Show方法

    对css中 display:none的对象有用,对visibility:hidden的对象无效.

  6. VM下redhat9.0不能上网

    近期本人在学习linux时,安装Red Hat Linux9后,可是上不了网,弄得查资料还得切换到虚拟机上去,特耗时间.还好没有疯掉! 首先,测试下你的linux看是否是这类问题,输入ping www ...

  7. GAN网络

    http://www.sohu.com/a/130252639_473283 高分辨率图像重建 https://zhuanlan.zhihu.com/p/25201511 生成式对抗网络GAN有哪些最 ...

  8. 第一百五十五节,封装库--JavaScript,轮播器

    封装库--JavaScript,轮播器 html <div id="banner"> <img src="img/banner1.jpg" a ...

  9. 第一百四十七节,封装库--JavaScript,滑动导航

    JavaScript,封装库--滑动导航 效果图 html <!--滑动导航--> <div id="nav"> <ul class="ab ...

  10. 【vijos】1789 String(组合计数+奇怪的题)

    https://vijos.org/p/1789 我yy了一下发现我的方法没错啊,为嘛才80分..(后来看了题解,噗,还要判断k>n和k=1的情况QAQ 当k=1的时候,答案显然是m^n 当k& ...