HttpURLConnection發送post請求信息

	public static void testHttpQuest() {
		// {'pfxInfo':'no','isPfx':'no','signInfo':'中文','passCode':'','signType':'yes'}
		logger.info("開始下載更新CRL");
		// 創建URL 對象
		URL url = null;
		byte[] b = null;
		FileOutputStream fos = null;
		InputStream is = null;
		HttpURLConnection httpUrlConnection = null;

		try {
			 url = new
			 URL("http://101.201.80.221:8899/VerifyInfoService/doSign");
//			url = new URL("http://localhost:8888//VerifyInfoService/doSign");
			// 獲取 httpUrl連接
			httpUrlConnection = (HttpURLConnection) url.openConnection();

			httpUrlConnection.setRequestMethod("POST");
			httpUrlConnection.setDoInput(true);
			httpUrlConnection.setDoOutput(true);
			String params = "signedInfoValue={'pfxInfo':'no','isPfx':'no','signInfo':'中文','passCode':'','signType':'yes'}";
			httpUrlConnection.setRequestProperty("accept", "*/*");
			httpUrlConnection.setRequestProperty("connection", "Keep-Alive");
			httpUrlConnection.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			 httpUrlConnection.connect();
			OutputStreamWriter outStream = new OutputStreamWriter(
					httpUrlConnection.getOutputStream(), "UTF-8");
			outStream.write(params);
			outStream.flush();
			outStream.close();
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(  
					httpUrlConnection.getInputStream())); 
			
			 String line;  
			 StringBuffer responseResult = new StringBuffer();  

	            while ((line = bufferedReader.readLine()) != null) {  
	            	responseResult.append(line); 
	            }  
	            System.out.println(responseResult.toString());
			
			int responseCode = httpUrlConnection.getResponseCode();  
			System.out.println(responseCode);
			httpUrlConnection.disconnect();
		} catch (MalformedURLException e) {
			logger.info("下載CRL列表失敗!");
			e.printStackTrace();
		} catch (IOException e) {
			logger.info("下載CRL列表失敗!");
			e.printStackTrace();
		}
	}


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