htttp请求,java实例
import java.io.InputStream; import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import com.fangwifi.domain.arinvoice.ArInvoice; //import net.sf.json.JSONObject; import com.fangwifi.domain.customer.CustomerQuery; public class HttpClientUtil { static DefaultHttpClient httpClient = new DefaultHttpClient(); public static String get(String url, String encoding) throws Exception { HttpGet httpGet = new HttpGet(url); HttpResponse res = httpClient.execute(httpGet); return getContent(res, encoding); } public static String get(String url, String encoding, DefaultHttpClient client) throws Exception { HttpGet httpGet = new HttpGet(url); HttpResponse res = client.execute(httpGet); return getContent(res, encoding); } public static String post(String url, StringEntity se, String host, String referer, String encoding) throws Exception { HttpPost httpPost = new HttpPost(url); httpPost.setEntity(se); httpPost.setHeader("Host", host); httpPost.setHeader("Referer", referer); httpPost.setHeader("Accept", "*/*"); httpPost.setHeader("Accept-Language", "zh-cn"); httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); httpPost.setHeader("UA-CPU", "x86"); httpPost.setHeader( "User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; InfoPath.2; CIBA)"); httpPost.setHeader("Connection", "close"); HttpResponse response = httpClient.execute(httpPost); return getContent(response, encoding); } public static String httpPost(String url, String queryString, String encoding) throws Exception { HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new StringEntity(queryString,"UTF-8")); httpPost.setHeader("Content-Type", "application/json;charset=utf-8"); httpPost.getParams().setParameter("http.socket.timeout", new Integer(60000)); httpPost.setHeader("Connection", "close"); HttpResponse response = httpClient.execute(httpPost); return getContent(response, encoding); } public static String getContent(HttpResponse res, String encoding) throws Exception { HttpEntity ent = res.getEntity(); String result = IOUtils.toString(ent.getContent(), encoding); ent.consumeContent(); return result; } public static InputStream getStream(String url) throws Exception { HttpGet httpGet = new HttpGet(url); HttpResponse res = httpClient.execute(httpGet); return res.getEntity().getContent(); } public static InputStream getStream(String url, DefaultHttpClient client) throws Exception { HttpGet httpGet = new HttpGet(url); httpGet.setHeader( "User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; InfoPath.2; CIBA)"); httpGet.setHeader("Referer", "http://reg.126.com/regmail126/userRegist.do?action=fillinfo"); // httpGet.setHeader("Accept", "*/*"); // httpGet.setHeader("Accept-Language", "zh-cn"); // httpGet.setHeader("Accept-Encoding", "gzip, deflate"); httpGet.setHeader("Connection", "close"); HttpResponse res = client.execute(httpGet); return res.getEntity().getContent(); } public static void main(String args[]) throws Exception { //访问地址 String url = "http://192.168.1.168:1234/customer/queryListByCode"; CustomerQuery query = new CustomerQuery(); query.setChannelsCode("xxxxxx"); //发送 String resultString = HttpClientUtil.httpPost(url, query.toJson(), "utf-8"); System.out.println(resultString); } }
注: 已测,可直接用
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!