最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Java發(fā)送HTTP請求

 更新時間:2019年03月29日 16:27:49   作者:胖虎。。  
這篇文章主要介紹了Java發(fā)送HTTP請求,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

請求http的Demo是個人親測過,目前該方式已經(jīng)在線上運行著。因為是http請求,所有發(fā)送post 和get 請求的demo都有在下方貼出,包括怎么測試,大家可直接 copy到自己的項目中使用。

正文

使用須知

為了避免大家引錯包我把依賴和涉及到包路徑給大家

import java.net.HttpURLConnection;
import java.net.URI;
 
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
 
 
import com.fasterxml.jackson.databind.ObjectMapper;
 <dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.4.8</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.3</version>
		</dependency>

HTTP 發(fā)送 get 請求

首先我們引入兩個包

發(fā)送get請求的工具類,可直接 copy 使用即可

另外,我拋出異常的代碼大家改成自己業(yè)務(wù)的異常,不需要就刪除掉。

參數(shù)說明:

host:ip

servUri:url

reString:參數(shù)

public static String getHttpData(String host, String servUri, String reString) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("getHttpData:host:" + host + ",servUri:" + servUri + ",reString:" + reString);
		String strResp = null;
		try {
			URI uri = new URIBuilder().setScheme("http").setHost(host).setPath(servUri)
					.setParameter("strInfo", reString).build();
			HttpGet httpGet = new HttpGet(uri);
			CloseableHttpClient client3 = HttpClients.createDefault();
			HttpResponse resp;
			resp = client3.execute(httpGet);
			if (resp.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
				strResp = EntityUtils.toString(resp.getEntity());
				logger.info("the return result:{}", strResp);
			} else {
				logger.info("Error Response:", resp.getStatusLine().toString());
				throw new CommonBusinessException(CommonConstants.TASK_RELEASE_WCF,
						CommonConstants.TASK_RELEASE_WCF_DESC);
			}
		} catch (Exception e) {
			logger.error(sb.toString() + ":" + e.getMessage(), e.getCause());
			throw new CommonBusinessException(CommonConstants.TASK_RELEASE_WCF, CommonConstants.TASK_RELEASE_WCF_DESC);
		}
		return strResp;
	}
 

HTTP 發(fā)送 post 請求

發(fā)送post分兩種,我分兩種的原因是為了讓大家方便,想傳對象和 json 可以直接復(fù)制過用就可以用,不用你們在轉(zhuǎn)了。

第一種是直接接收json

參數(shù)明說:

url:url

json:參數(shù)

public static String doPostData(String url, String json) throws Exception {
		DefaultHttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		String result = "";
		HttpResponse res = null;
		try {
			StringEntity s = new StringEntity(json.toString(), "UTF-8");
			s.setContentType("application/json");
			post.setHeader("Accept", "application/json");
			post.setHeader("Content-type", "application/json; charset=utf-8");
			post.setEntity(s);
			res = client.execute(post);
			if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				result = EntityUtils.toString(res.getEntity());
				return HttpStatus.SC_OK + "";
			}
		} catch (Exception e) {
			if(res == null) {
				return "HttpResponse 為 null!";
			}
			throw new RuntimeException(e);
		}
		if(res == null || res.getStatusLine() == null) {
			return "無響應(yīng)";
		}
		return res.getStatusLine().getStatusCode() + "";
	}
@Test
  public void test12() throws Exception {
    String HOST = "http://eipwcf.aspirecn.com/SvcEF/Service1.svc/WCF_EF_MSA_GetDataInfo_P";
    HttpClient client = new HttpClient();
    JSONObject json = new JSONObject();
    json.put("msgId", msgId);
    String reslut=client.doPostData(HOST, json);
  }
 

第二種是參數(shù)是對象

參數(shù)說明:

url:url

tram:對象

public static String doHttpPostData(String url, TaskReleaseApprovalModel tram)
			throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("doHttpPostData:url:" + url + ",tram:" + tram.toString() + ",contentType:" + contentType);
		logger.info(sb.toString());
		String tmpString = "";
		HttpPost request = new HttpPost(url);
		request.setHeader("Accept", "application/json");
		request.setHeader("Content-type", "application/json");
		ObjectMapper mapper = new ObjectMapper();
		String jsonString;
		try {
			jsonString = mapper.writeValueAsString(tram);
			StringEntity entity = new StringEntity(jsonString, "UTF-8");
			request.setEntity(entity);
			CloseableHttpClient client = HttpClients.createDefault();
			HttpResponse response = client.execute(request);
			if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
				tmpString = EntityUtils.toString(response.getEntity());
				logger.info("the post result:tmpString:{}", tmpString);
			} else {
				logger.info("the post failure:tmpString:", tmpString);
				throw new CommonBusinessException(CommonConstants.TASK_RELEASE_WCF,
						CommonConstants.TASK_RELEASE_WCF_DESC);
			}
		} catch (Exception e) {
			logger.error(sb.toString() + ":" + e.getMessage(), e.getCause());
			throw new CommonBusinessException(CommonConstants.TASK_RELEASE_POSTWCF,
					CommonConstants.TASK_RELEASE_POSTWCF_DESC);
		}
		return tmpString;
	}

這個方法我想不用寫測試類大家也會用,傳過去對象和地址就可以了,很方便很簡單。

以上所述是小編給大家介紹的Java發(fā)送HTTP請求詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 一不小心就讓Java開發(fā)踩坑的fail-fast是個什么鬼?(推薦)

    一不小心就讓Java開發(fā)踩坑的fail-fast是個什么鬼?(推薦)

    這篇文章主要介紹了Java fail-fast,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • JDBC程序更新數(shù)據(jù)庫中記錄的方法

    JDBC程序更新數(shù)據(jù)庫中記錄的方法

    這篇文章主要介紹了JDBC程序更新數(shù)據(jù)庫中記錄的方法,涉及Java基于JDBC操作數(shù)據(jù)庫的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • Java源碼解析ThreadLocal及使用場景

    Java源碼解析ThreadLocal及使用場景

    今天小編就為大家分享一篇關(guān)于Java源碼解析ThreadLocal及使用場景,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Java中的cglib代理詳解

    Java中的cglib代理詳解

    這篇文章主要介紹了Java中的cglib代理詳解, 代理模式是一種設(shè)計模式,它可以為其他對象提供一種代理,以控制對該對象的訪問,可以在運行時動態(tài)地創(chuàng)建代理對象,而不需要手動編寫代理類的代碼,需要的朋友可以參考下
    2023-09-09
  • 自己手寫Mybatis通用batchInsert問題

    自己手寫Mybatis通用batchInsert問題

    這篇文章主要介紹了自己手寫Mybatis通用batchInsert問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Java方法反射實現(xiàn)原理詳解

    Java方法反射實現(xiàn)原理詳解

    這篇文章主要為大家詳細(xì)介紹了Java方法反射的實現(xiàn)原理,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • JDK19新特性使用實例詳解

    JDK19新特性使用實例詳解

    這篇文章主要為大家介紹了JDK19新特性使用實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • SpringBoot項目打成War布署在Tomcat的詳細(xì)步驟

    SpringBoot項目打成War布署在Tomcat的詳細(xì)步驟

    這篇文章主要介紹了SpringBoot項目打成War布署在Tomcat,本文分步驟結(jié)合圖文實例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • IntelliJ IDEA中新建Java class的解決方案

    IntelliJ IDEA中新建Java class的解決方案

    今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中新建Java class的解決方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • java教程之java繼承示例詳解

    java教程之java繼承示例詳解

    這篇文章主要介紹了java繼承示例詳解,需要的朋友可以參考下
    2014-04-04

最新評論

保德县| 德州市| 新源县| 特克斯县| 赤水市| 旺苍县| 宁德市| 达拉特旗| 特克斯县| 上饶县| 水城县| 广德县| 临汾市| 焉耆| 房产| 闸北区| 宁乡县| 寿宁县| 邯郸县| 沾益县| 太仓市| 台东市| 赞皇县| 镇雄县| 海伦市| 崇信县| 措勤县| 三台县| 广昌县| 鹤庆县| 崇义县| 文山县| 永安市| 当阳市| 克什克腾旗| 丽江市| 宣武区| 呈贡县| 宾川县| 房产| 定州市|