Java 以post请求方式通过json格式调用Webservice接口

本文详细介绍了如何通过GET请求调用Discuz的缓存清除功能,并提供了Java发送GET请求的示例代码。同时,展示了如何在PHP端接收并解析Java发送过来的JSON数据。最后,附上了实际应用中的GET请求示例,帮助开发者理解和实现类似功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

打算用post请求发送json参数调用discuz的清除缓存的功能,但是discuz不允许使用post请求,不想改太多discuz的代码,最后只能用get请求发送,虽然没用到,还是总结下,以后会用到:

Java发送post请求:


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
URL url = null ;
try {
     // 创建连接
     HttpURLConnection conn = (HttpURLConnection) url.openConnection();
     
     conn.setDoOutput( true );
     conn.setDoInput( true );
     conn.setUseCaches( false );
     conn.setRequestMethod( "POST" );
     conn.setRequestProperty( "Content-type" , "application/x-www-form-urlencoded" );
     conn.connect();
     
     // json参数
     DataOutputStream out = new DataOutputStream(conn.getOutputStream());
             
     JSONObject args = new JSONObject();
     args.put( "method" , "open" );
     out.writeBytes(args.toString());
     out.flush();
     out.close();
     
     // 获取响应
     BufferedReader reader = new BufferedReader( new InputStreamReader(conn.getInputStream()));
     String lines;
     StringBuffer sb = new StringBuffer();
     while ((lines = reader.readLine()) != null ){
         lines = new String(lines.getBytes(), "utf-8" );
         sb.append(lines);
     }
     reader.close();
     
     System.out.println( "-----------------------------------------------------" );
     System.out.println(sb);
     System.out.println( "-----------------------------------------------------" );
     
     // 关闭连接
     conn.disconnect();
} catch (Exception e) {
     e.printStackTrace();
}
PHP接收java post传递的json:



?
1
2
3
4
5
6
7
8
9
10
11
12
// 接收参数
$str = file_get_contents ( "php://input" );
// 转为json
$json = json_decode( $str );
 
// json转array
$params = array ();
foreach ( $json as $key => $value ) {
     $params [ $key ] = $value ;
}
 
print_r(params);


下面是我这次用到的Get请求:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
URL url = null ;
try {
     // 创建连接
     HttpURLConnection conn = (HttpURLConnection) url.openConnection();
     
     // 获取响应
     BufferedReader reader = new BufferedReader( new InputStreamReader(conn.getInputStream()));
     String lines;
     StringBuffer sb = new StringBuffer();
     while ((lines = reader.readLine()) != null ){
         lines = new String(lines.getBytes(), "utf-8" );
         sb.append(lines);
     }
     reader.close();
     
     System.out.println( "-----------------------------------------------------" );
     System.out.println(sb);
     System.out.println( "-----------------------------------------------------" );
     
     // 关闭连接
     conn.disconnect();
} catch (Exception e) {
     e.printStackTrace();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值