java如何给args传参

第一步:打开Edit Configurations…
在这里插入图片描述
第二步:往Program aguments里面写你要传的args
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 中使用 HTTP POST 请求传递参数可以使用 HttpURLConnection 或 HttpClient 客户端库。以下是使用 HttpURLConnection 的示例代码: ``` import java.net.HttpURLConnection; import java.net.URL; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStreamReader; public class HttpPostExample { public static void main(String[] args) { try { URL url = new URL("http://example.com/post"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setDoOutput(true); String parameters = "param1=value1&param2=value2"; OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write(parameters); writer.flush(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } writer.close(); reader.close(); connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们使用 HttpURLConnection 发送 HTTP POST 请求。我们设置请求方法为 POST,设置 Content-Type 为 application/x-www-form-urlencoded(这是 HTTP POST 请求默认的 Content-Type),设置 setDoOutput 为 true,以便我们可以向服务器发送参数。然后,我们将参数作为字符串写入输出流中,并刷新流。最后,我们从服务器读取响应。 如果您想使用 HttpClient 客户端库进行 HTTP POST 请求,请参考以下示例代码: ``` import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class HttpPostExample { public static void main(String[] args) { try { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://example.com/post"); String parameters = "param1=value1&param2=value2"; StringEntity entity = new StringEntity(parameters); httpPost.setEntity(entity); CloseableHttpResponse response = httpclient.execute(httpPost); try { HttpEntity responseEntity = response.getEntity(); System.out.println(EntityUtils.toString(responseEntity)); } finally { response.close(); } } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们使用 HttpClient 客户端库发送 HTTP POST 请求。我们创建一个 CloseableHttpClient 对象,然后创建一个 HttpPost 对象并设置请求 URL。我们将参数作为字符串创建 StringEntity 对象,并设置为 HttpPost 的实体。最后,我们执行 HTTP POST 请求,并从服务器读取响应。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值