HTTP request garbled solution one
This is also the simplest way, mainly using the IOUtils
tool class.
URL realUrl = new URL(url); // 打开和URL之间的连接 conn = (HttpURLConnection) realUrl.openConnection(); // 发送POST请求必须设置如下两行 conn.setRequestMethod(StringUtils.upperCase(type));// 提交模式 conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestProperty("Connection", "close"); conn.setConnectTimeout(3000); //设置连接主机超时(单位:毫秒) conn.setReadTimeout(2000); //设置从主机读取数据超时(单位:毫秒) //省略一些代码 //获取InputStream InputStream inStream = conn.getInputStream(); //通过IOUtils 转化,把流转换为字符串。 //charset为编码格式,可选为"utf-8","gbk","gb2312"等等 String result = IOUtils.toString(inStream,charset);
Recommended online video tutorial: java course
HTTP request garbled solution two
Next, use the above code, use our most commonly used method, read line by line, the charset is the same as above.
StringBuffer sb = new StringBuffer(""); in = new BufferedReader(new InputStreamReader(inStream, charset)); String line = ""; while ((line = in.readLine()) != null){ sb.append(line); } result = sb.toString();
If you want to know more about java, please visit: Introduction to java language
The above is the detailed content of How to solve garbled http request in java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!
