post請求方式寫法--json和表單提交

 下面第一個 是json類型的

public static String send(String url,String data) throws DJException{
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost post = null;
		CloseableHttpResponse response=null;
		try {
			post = new HttpPost(url);  // 請求方法Post
			StringEntity entity = new StringEntity(data,"utf-8");
			entity.setContentEncoding("UTF-8");    // 字符編碼
			entity.setContentType("application/json");   // json類型
			post.setEntity(entity);
			response =httpClient.execute(post);
			int statusCode=	response.getStatusLine().getStatusCode(); // 返回的狀態碼
			if(statusCode == 200) {
				HttpEntity he = response.getEntity();
				return EntityUtils.toString(he, "UTF-8");
			}else{
				throw new DJException("服務器連接異常statusCode:"+statusCode);
			}
		} catch (IOException e) {
			e.printStackTrace();
			throw new DJException("請求失敗:"+e.getMessage());
		}finally {
			try {
				response.close();
				httpClient.close();
			} catch (IOException e) {
			}	
		}
		
	}

第二個是傳入的參數是表單類型的  兩種方式,區別不大

類型爲from-data數據格式的請求方式
 /**
     * 類型爲from-data數據格式的請求方式
     * @param url
     * @return
     * @throws Exception
     */
	public static String send(String url,Map<String,String> map) throws Exception{
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost post = null;
		CloseableHttpResponse response=null;
		try {
			post = new HttpPost(url);
            // params添加表單數據 作爲參數
			List<NameValuePair> params = new ArrayList();
			for (String parameter: map.keySet()) {
				params.add(new BasicNameValuePair(parameter, map.get(parameter)));
			}
            UrlEncodedFormEntity  entity = new UrlEncodedFormEntity(params,"UTF-8");
			entity.setContentEncoding("UTF-8");
			entity.setContentType("application/x-www-form-urlencoded");
			post.setEntity(entity);
			response =httpClient.execute(post);
			int statusCode=	response.getStatusLine().getStatusCode();
			if(statusCode == 200) {
				HttpEntity he = response.getEntity();
				return EntityUtils.toString(he, "UTF-8");
			}else{
				throw new DJException("服務器連接異常statusCode:"+statusCode);
			}
		} catch (IOException e) {
			e.printStackTrace();
			throw new DJException("請求失敗:"+e.getMessage());
		}finally {
			try {
				response.close();
				httpClient.close();
			} catch (IOException e) {
			}
		}

	}



	/*
	 * **HttpClient Post 以表單提交方式請求 帶參數**
	 */
	public static String send2(String url,Map<String,Object> map) throws DJException, IOException{
		//1、創建HttpClient
		org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
		//2、創建get或post請求方法
		PostMethod method = new PostMethod(url);
		//3、設置編碼
		httpClient.getParams().setContentCharset("UTF-8");
		//4、設置請求消息頭,爲表單方式提交
		method.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
		Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, Object> entry = it.next();
			//5、設置參數
			//method.setRequestHeader(key,value);
			method.setParameter(entry.getKey(), entry.getValue().toString());
		}


//		6、執行提交
		httpClient.executeMethod(method);
		int statusCode= method.getStatusLine().getStatusCode();
		if(statusCode == 200) {
			String responseBodyAsString = method.getResponseBodyAsString();
			return responseBodyAsString;
		}else{
			throw new DJException("服務器連接異常statusCode:"+statusCode);
		}
	}

其他資料:https://www.cnblogs.com/hanyj123/p/9641626.html

https://www.cnblogs.com/shengwei/p/5527394.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章