使用httpclient代理服务和传递参数以及返回response
时间: 2024-04-18 21:33:00 浏览: 180
采用httpclient上传数据
使用 HttpClient 来代理服务并传递参数以及获取响应结果的代码示例如下:
```java
import java.io.IOException;
import.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.util.ArrayList;
import java.util.List;
public class HttpClientExample {
public static void main(String[] args) {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
try {
// GET 请求示例
String getEndpoint = "https://api.example.com/endpoint";
String getRequestParams = "?param1=value1¶m2=value2";
String getUrl = getEndpoint + getRequestParams;
HttpGet httpGet = new HttpGet(getUrl);
HttpResponse getResponse = httpClient.execute(httpGet);
HttpEntity getEntity = getResponse.getEntity();
if (getEntity != null) {
InputStream inputStream = getEntity.getContent();
// 处理输入流,获取响应结果
String responseString = convertInputStreamToString(inputStream);
System.out.println("GET 响应结果:" + responseString);
}
// POST 请求示例
String postEndpoint = "https://api.example.com/endpoint";
HttpPost httpPost = new HttpPost(postEndpoint);
// 设置 POST 请求参数
List<NameValuePair> postParams = new ArrayList<>();
postParams.add(new BasicNameValuePair("param1", "value1"));
postParams.add(new BasicNameValuePair("param2", "value2"));
httpPost.setEntity(new UrlEncodedFormEntity(postParams));
HttpResponse postResponse = httpClient.execute(httpPost);
HttpEntity postEntity = postResponse.getEntity();
if (postEntity != null) {
InputStream inputStream = postEntity.getContent();
// 处理输入流,获取响应结果
String responseString = convertInputStreamToString(inputStream);
System.out.println("POST 响应结果:" + responseString);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException {
StringBuilder result = new StringBuilder();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.append(new String(buffer, 0, length, StandardCharsets.UTF_8));
}
return result.toString();
}
}
```
上述代码使用 Apache HttpClient 来发送 GET 和 POST 请求。你可以根据实际情况进行修改和调整,以适应你的代理服务和传递的参数。获取到响应结果后,你可以根据需要进行进一步处理。
阅读全文