Java发送post请求及其所需的jar包
摘要
介绍如何使用Java发送post请求,以及发送post请求所需的jar包。我们将讨论发送post请求的基本原理,然后详细介绍6个方面的内容:1、使用HttpURLConnection发送post请求;2、使用HttpClient发送post请求;3、发送json格式的post请求;4、发送文件的post请求;5、发送表单数据的post请求;6、发送xml格式的post请求。我们将对全文进行总结归纳。
使用HttpURLConnection发送post请求
在Java中,可以使用HttpURLConnection类来发送post请求。需要创建一个URL对象,然后通过openConnection方法获取HttpURLConnection对象。接下来,设置请求方法为POST,设置请求头信息,获取输出流并写入请求参数,最后获取输入流获取响应结果。以下是一个简单的示例代码:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpURLConnectionExample {
public static void main(String[] args) throws Exception {
String url = "
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "param1=value1¶m2=value2";
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}
使用HttpClient发送post请求
除了使用HttpURLConnection,还可以使用Apache HttpClient库来发送post请求。HttpClient提供了更加方便的API来发送HTTP请求和处理响应。以下是一个简单的示例代码:
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class HttpClientExample {
public static void main(String[] args) throws IOException {
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("
post.setHeader("User-Agent", "Mozilla/5.0");
post.setHeader("Accept-Language", "en-US,en;q=0.5");
HttpResponse response = client.execute(post);
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
}
发送json格式的post请求
当需要发送json格式的post请求时,需要设置请求头信息为"application/json",并将json字符串作为请求参数发送。以下是一个简单的示例代码:
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
public class JsonPostExample {
public static void main(String[] args) throws IOException {
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("
post.setHeader("Content-Type", "application/json");
String json = "{"key1":"value1","key2":"value2"}";
StringEntity entity = new StringEntity(json);
post.setEntity(entity);
HttpResponse response = client.execute(post);
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
}
发送文件的post请求
如果需要发送文件的post请求,可以使用MultipartEntity来构建请求实体,并将文件添加到请求中。以下是一个简单的示例代码:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.File;
import java.io.IOException;
public class FilePostExample {
public static void main(String[] args) throws IOException {
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("file", new File("file.txt"));
HttpEntity entity = builder.build();
post.setEntity(entity);
HttpResponse response = client.execute(post);
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
}
发送表单数据的post请求
发送表单数据的post请求与发送json格式的post请求类似,只需要设置请求头信息为"application/x-www-form-urlencoded",并将表单数据作为请求参数发送。以下是一个简单的示例代码:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.IOException;
public class FormPostExample {
public static void main(String[] args) throws IOException {
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
String data = "param1=value1¶m2=value2";
StringEntity entity = new StringEntity(data);
post.setEntity(entity);
HttpResponse response = client.execute(post);
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
}
发送xml格式的post请求
发送xml格式的post请求与发送json格式的post请求类似,只需要设置请求头信息为"application/xml",并将xml字符串作为请求参数发送。以下是一个简单的示例代码:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.IOException;
public class XmlPostExample {
public static void main(String[] args) throws IOException {
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("
post.setHeader("Content-Type", "application/xml");
String xml = "value1value2";
StringEntity entity = new StringEntity(xml);
post.setEntity(entity);
HttpResponse response = client.execute(post);
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
}
总结归纳
使用Java发送post请求的方法,并详细讨论了使用HttpURLConnection和HttpClient发送post请求的示例代码。还发送json格式、文件、表单数据和xml格式的post请求的示例代码。通过的学习,读者可以掌握Java发送post请求的基本原理和常用方法,以及相应的jar包的使用方法。希望能够帮助读者更好地理解和使用Java发送post请求。
// 来源:https://www.nzw6.com