Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
LearnJava
/
Advanced
/
src
/
io
/
HttpTools.java
LearnJava
/
Advanced
/
src
/
io
/
HttpTools.java
HttpTools.java 4.94 KB
Copy Edit Raw Blame History
yswift authored 2019年04月07日 16:47 +08:00 . init
package io;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.GZIPInputStream;
public class HttpTools {
private static final HashMap<String, String> EmptyProps = new HashMap<String, String>();
public static String doGetString(String url) throws IOException {
return doGetString(url, EmptyProps, "UTF-8");
}
public static String doGetString(String url, String charset) throws IOException {
return doGetString(url, EmptyProps, charset);
}
public static String doGetString(String url, Map<String, String> props) throws IOException {
return doGetString(url, props, "UTF-8");
}
public static String doGetString(String url, Map<String, String> props, String charset) throws IOException {
InputStream is = doGet(url, props);
String s = readString(is, charset);
is.close();
return s;
}
public static byte[] doGetBytes(String url) throws IOException {
return doGetBytes(url, new HashMap<String, String>());
}
public static byte[] doGetBytes(String url, Map<String, String> props) throws IOException {
InputStream is = doGet(url, props);
byte[] s = readBytes(is);
is.close();
return s;
}
public static InputStream doGet(String url) throws IOException {
return doGet(url, EmptyProps);
}
public static InputStream doGet(String url, Map<String, String> props) throws IOException {
// 把参数附加到url上
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> e : props.entrySet()) {
if (sb.length() > 0) {
sb.append("&");
}
sb.append(e.getKey()).append("=").append(e.getValue());
}
if (sb.length() > 0) {
url += "?" + sb.toString();
}
URL uurl = new URL(url);
URLConnection urlc = uurl.openConnection();
InputStream is = urlc.getInputStream();
// 检查获取的结果是否是被压缩过的
if ("gzip".equals(urlc.getContentEncoding())) {
// 如果是用GZIPInputStream包裹节压缩
is = new GZIPInputStream(is);
}
return is;
}
// POST
public static String doPostString(String url) throws IOException {
return doPostString(url, new HashMap<String, String>(), "UTF-8");
}
public static String doPostString(String url, String charset) throws IOException {
return doPostString(url, new HashMap<String, String>(), charset);
}
public static String doPostString(String url, Map<String, String> props) throws IOException {
return doPostString(url, props, "UTF-8");
}
public static String doPostString(String url, Map<String, String> props, String charset) throws IOException {
InputStream is = doPost(url, props);
String s = readString(is, charset);
is.close();
return s;
}
public static byte[] doPostBytes(String url) throws IOException {
return doPostBytes(url, new HashMap<String, String>());
}
public static byte[] doPostBytes(String url, Map<String, String> props) throws IOException {
InputStream is = doPost(url, props);
byte[] s = readBytes(is);
is.close();
return s;
}
public static InputStream doPost(String url, Map<String, String> props) throws IOException {
URL uurl = new URL(url);
URLConnection urlc = uurl.openConnection();
for (Map.Entry<String, String> e : props.entrySet()) {
urlc.setRequestProperty(e.getKey(), e.getValue());
}
// 发送POST请求必须设置如下两行
urlc.setDoOutput(true);
urlc.setDoInput(true);
InputStream is = urlc.getInputStream();
// 检查获取的结果是否是被压缩过的
if ("gzip".equals(urlc.getContentEncoding())) {
// 如果是用GZIPInputStream包裹节压缩
is = new GZIPInputStream(is);
}
return is;
}
/**
* 从流中读取所有内容,直到流结束,并转换为String格式。
*
* @param is
* 输入流
* @return 读取到字符串
* @throws IOException
*/
private static String readString(InputStream is, String charset) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
readTo(is, bos);
return bos.toString(charset);
}
/**
* 流中读取所有内容,直到流结束。
* @param is
* @return byte数组
* @throws IOException
*/
private static byte[] readBytes(InputStream is) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
readTo(is, bos);
return bos.toByteArray();
}
/**
* 从流中读取所有内容,并写入到另外的流中。
*
* @param is
* 输入流
* @param os
* 输出流
* @throws IOException
*/
private static void readTo(InputStream is, OutputStream os) throws IOException {
// 定义读取的缓冲区
final int len = 1024;
byte data[] = new byte[len];
while (true) {
int rlen = is.read(data);
if (rlen == -1) {
break;
}
os.write(data, 0, rlen);
}
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

Cancel

Releases

No release

Contributors

All

Language(Optional)

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/yswift/LearnJava.git
git@gitee.com:yswift/LearnJava.git
yswift
LearnJava
LearnJava
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /