请求http://192.168.10.123:41002/wechatpay/payment?amt=1000&branchId=1000000002&ccy=CNY的java方式
时间: 2024-12-23 12:19:51 浏览: 8
在Java中发送HTTP请求到`http://192.168.10.123:41002/wechatpay/payment?amt=1000&branchId=1000000002&ccy=CNY`通常会使用HttpClient库或者其他网络客户端库,比如Apache HttpClient、OkHttp或者Spring框架自带的RestTemplate。下面是一个简单的示例,使用的是Apache HttpClient:
```java
import org.apache.http.HttpHost;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class WechatPayRequest {
private static final String URL = "http://192.168.10.123:41002/wechatpay/payment";
public static void main(String[] args) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpGet request = new HttpGet(URL);
// 设置URL中的查询参数
request.setParams(setUrlParameters(request.getURI(),
"amt", "1000",
"branchId", "1000000002",
"ccy", "CNY"));
// 发送请求并获取响应
CloseableHttpResponse response = httpClient.execute(request);
// 处理响应...
response.close();
} finally {
httpClient.close();
}
}
// 助手函数用于设置GET请求的URL参数
private static void setUrlParameters(java.net.URI uri, String... paramNamesAndValues) {
for (String pair : paramNamesAndValues) {
int splitIndex = pair.indexOf('=');
if (splitIndex > 0) {
uri.setParameter(pair.substring(0, splitIndex), pair.substring(splitIndex + 1));
}
}
}
}
```
在这个例子中,我们创建了一个HttpGet请求,设置了URL参数,然后通过HttpClient发送请求。请注意,在实际应用中,可能需要处理异常,并且处理返回的CloseableHttpResponse。
阅读全文