java http数据传输_Java 模拟发送 http 请求并传输 JSON 数据

前言

近日在做安卓的服务端开发,由于对安卓一窍不通,又需要测试服务器功能,于是想着用 java 来模拟对服务器的请求。实现以 JSON 为主体的数据交互。以下代码参考了zhuawang's blog。

代码

首先通过url建立一个连接

URL realUrl = new URL("123.123.123.123:8080/test/");

// 打开和URL之间的连接

URLConnection conn = realUrl.openConnection();

接着设置以下请求的属性

//注意在传送 json 数据时注意设置 Content-Type 的值

conn.setRequestProperty("Content-Type", "application/json");

conn.setRequestProperty("connection", "keep-alive");

// 传送 json 数据一般是用 POST

// 发送 POST 请求必须设置如下两行

conn.setDoOutput(true);

conn.setDoInput(true);

最后写入要传输的参数

// 获取URLConnection对象对应的输出流

out = new PrintWriter(conn.getOutputStream());

// 发送请求参数

out.write(param);

// flush输出流的缓冲

out.flush();

最后你可以看看发出的请求返回了什么样的响应结果

in = new BufferedReader(

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用HttpURLConnection类或HttpClient库来模拟HTTP请求,实现登录系统并获取请求JSON数据。 使用HttpURLConnection类进行模拟HTTP请求,可以按照以下步骤进行操作: 1. 导入相关的包:import java.net.HttpURLConnection; import java.net.URL; import java.io.BufferedReader; import java.io.InputStreamReader; 2. 创建URL对象,指定要发送请求的URL地址:URL url = new URL("http://example.com/login"); 3. 打开连接,获取HttpURLConnection对象:HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 4. 设置请求的方法为POST:connection.setRequestMethod("POST"); 5. 设置请求的头部信息(如果需要):connection.setRequestProperty("Content-Type", "application/json"); 6. 设置是否允许输入输出:connection.setDoInput(true); connection.setDoOutput(true); 7. 构造请求数据,并发送请求:String requestData = "{\"username\":\"your_username\", \"password\":\"your_password\"}"; OutputStream outputStream = connection.getOutputStream(); outputStream.write(requestData.getBytes("UTF-8")); outputStream.close(); 8. 获取响应的数据:int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; StringBuilder response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); String jsonResponse = response.toString(); // 响应的JSON数据 } 使用HttpClient库进行模拟HTTP请求,可以按照以下步骤进行操作: 1. 导入相关的包:import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; 2. 创建HttpClient对象:HttpClient httpClient = HttpClientBuilder.create().build(); 3. 创建HttpPost对象,指定要发送请求的URL地址:HttpPost httpPost = new HttpPost("http://example.com/login"); 4. 设置请求的头部信息(如果需要):httpPost.setHeader("Content-Type", "application/json"); 5. 构造请求数据,并设置到HttpPost对象中:String requestData = "{\"username\":\"your_username\", \"password\":\"your_password\"}"; StringEntity requestEntity = new StringEntity(requestData, "UTF-8"); httpPost.setEntity(requestEntity); 6. 发送请求,并获取响应:HttpResponse response = httpClient.execute(httpPost); HttpEntity responseEntity = response.getEntity(); String jsonResponse = EntityUtils.toString(responseEntity, "UTF-8"); // 响应的JSON数据 以上是使用Java模拟HTTP请求、登录系统并获取请求JSON数据的简单示例。根据具体的需求和情况,还可以根据服务器的要求设置其他的请求头部信息,处理响应的状态码等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值