Java中模拟HTTP请求
1.基于HttpURLConnection
2.基于HttpClient (httpclient4.2.3 httpcore4.2.2 HttpClient支持异步,需要Jar包)
public class HttpUtil { private final static Logger logger = LogManager.getLogger(HttpUtil.class); /** * 支持普通HTTP请求 WCF WEBSERVICE * * @param url * @param xml * WCF XML参数 * @param method * @param contentType * @param timeOut * @return */ public static String HttpProxy(String url, String xml, String method, String contentType, String timeOut) { OutputStream os = null; HttpURLConnection conn = null; BufferedReader reader = null; StringBuffer sb = null; try { if (StringUtils.isEmpty(method)) { method = "GET"; } if (StringUtils.isEmpty(contentType)) { contentType = "application/x-www-form-urlencoded";// "application/x-java-serialized-object" } if (StringUtils.isEmpty(timeOut)) { timeOut = "5000"; } URL _url = new URL(url); conn = (HttpURLConnection) _url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod(method); conn.setRequestProperty("Content-type", contentType); conn.setConnectTimeout(Integer.parseInt(timeOut)); conn.setRequestProperty("Content-type", contentType); // conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); // 请求参数 if (!StringUtils.isEmpty(xml)) { os = conn.getOutputStream(); os.write(xml.getBytes()); os.flush(); } // 返回值 if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) { conn.connect(); reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); sb = new StringBuffer(); String str = null; while ((str = reader.readLine()) != null) { sb.append(str + "\n"); } } return sb.toString(); } catch (IOException ex) { logger.info("url:" + url + " exception:" + ex.getMessage()); } finally { try { if (conn != null) { conn.disconnect(); } if (reader != null) { reader.close(); } if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; } /** * HttpClient Get请求 * * @param url * @param encoding * @return */ public static String HttpGetProxy(String url, String encoding) { HttpClient client = new DefaultHttpClient(); // 设置为get取连接的方式. HttpGet get = new HttpGet(url); HttpResponse response; String str = null; StringBuffer sb = new StringBuffer(); BufferedReader reader = null; if (StringUtils.isEmpty(encoding)) { encoding = "UTF-8"; } try { response = client.execute(get); HttpEntity entity = response.getEntity(); if (entity != null) { // 得到返回的主体内容. reader = new BufferedReader(new InputStreamReader(entity.getContent(), encoding)); while ((str = reader.readLine()) != null) { sb.append(str + "\n"); } } return sb.toString(); } catch (Exception ex) { logger.info("url:" + url + " exception:" + ex.getMessage()); } finally { try { client.getConnectionManager().shutdown(); reader.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } /** * HttpClient Post请求 带参数 * * @param url * @param encoding * @return */ @SuppressWarnings("unchecked") public static String HttpPostProxy(String url, Map params, String encoding) { DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse response; BufferedReader reader = null; StringBuffer sb = new StringBuffer(); if (StringUtils.isEmpty(encoding)) { encoding = "UTF-8"; } // 请求参数 List<NameValuePair> postParams = new ArrayList<NameValuePair>(); if (params != null && params.keySet().size() > 0) { Iterator iterator = params.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Entry) iterator.next(); postParams.add(new BasicNameValuePair((String) entry.getKey(), (String) entry.getValue())); } } HttpPost httpost = new HttpPost(url); httpost.setHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");// "application/x-www-form-urlencoded" httpost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0"); httpost.setHeader("Accept", "text/html, */*"); httpost.setEntity(new UrlEncodedFormEntity(postParams, Consts.UTF_8)); try { try { response = httpclient.execute(httpost); HttpEntity entity = response.getEntity(); System.out.println("Login form get: " + response.getStatusLine()); if (entity != null) { reader = new BufferedReader(new InputStreamReader(entity.getContent(), encoding)); String str = ""; while ((str = reader.readLine()) != null) { sb.append(str + "\n"); } } // 处理Cookies List<Cookie> cookies = httpclient.getCookieStore().getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("Cookie: - " + cookies.get(i).toString()); } } return sb.toString(); } catch (IOException ex) { logger.info("url:" + url + " exception:" + ex.getMessage()); } } finally { try { httpclient.getConnectionManager().shutdown(); reader.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }
Just Test
@Test public void TestHttp() { // getInput(); // getUrl("http://cnblogs.com/", "utf-8"); // System.out.println(HttpUtil.HttpProxy("http://cnblogs.com/", "", "", "", "1000")); // System.out.println(HttpUtil.HttpGetProxy("http://cnblogs.com/", "")); // WCF // String url = "http://api.homeinns.com/CWAP_beta2/webs/GetHotelNmByCityCD.aspx"; // StringBuffer param_xml = new StringBuffer(500); // param_xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("<CwapMessage xmlns=\"http://wap.homeinns.com/cwap\">").append( // "<version>1.0</version>").append("<TxtMsgMessage>").append("<Lisence>800332</Lisence>").append("<CityCD>0210</CityCD>").append( // "</TxtMsgMessage>").append("</CwapMessage>"); // System.out.println(HttpUtil.HttpProxy(url, param_xml.toString(), null, null, null)); Map<String, String> params = new HashMap<String, String>(); params.put("beginDate", "2013-04-21"); params.put("cityName", "上海"); params.put("del_session_flag", "false"); params.put("endDate", "2013-04-22"); params.put("pn", "1"); params.put("range", "50000"); params.put("sCityCd", "0210"); params.put("promotion", ""); params.put("sHotelBrand", ""); params.put("sLandMark", ""); params.put("sLandMarkName", ""); params.put("sRegion", ""); params.put("searchFlag", ""); params.put("sortField", ""); params.put("strlatlon", ""); params.put("subwayId", ""); System.out.println(HttpUtil.HttpPostProxy("http://www.homeinns.com/loadHotel.html", params, "")); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述