httpclient可以跨語言調用第三方接口嗎?
簡述
本文主要介紹如何基于HttpClient接口調用。
所需jar實例package httputil_demo;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.NameValuePair;import org.apache.commons.httpclient.methods.PostMethod;import com.alibaba.fastjson.JSONObject;/**** <p>Title: HttputilDemo</p>** <p>Description: http接口請求實例</p>** @author** @date 2020年6月1日**/public class HttputilDemo {public static void main(String[] args) {String Url = "接口地址";JSONObject jsonparam = new JSONObject();jsonparam.put("key", "value");HttpClient client = new HttpClient();PostMethod method = new PostMethod(Url);client.getParams().setContentCharset("UTF-8");method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");List<NameValuePair> paramList = new ArrayList<NameValuePair>();for (String key : jsonparam.keySet()) {paramList.add(new NameValuePair(key, jsonparam.getString(key)));}NameValuePair[] array = paramList.toArray(new NameValuePair[paramList.size()]);method.setRequestBody(array);String SubmitResult = "";try {client.executeMethod(method);SubmitResult = method.getResponseBodyAsString();System.out.println(SubmitResult);} catch (IOException e) {e.printStackTrace();} finally {method.releaseConnection();}}}
請求結果代碼目錄如果解決了你的疑惑,請點點關注和評論,謝謝支持。
正春華枝俏,待秋實果茂,與君共勉。