HttpURLConnection发送post请求信息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
		}
	}


以上是关于HttpURLConnection发送post请求信息的主要内容,如果未能解决你的问题,请参考以下文章

java 使用原生HttpURLConnection发送post请求

Java-使用HttpURLConnection发送GET,POST请求

如何使用 HttpURLConnection 在请求正文中发送数据?

java模拟post请求发送json

Java利用原始HttpURLConnection发送http请求数据小结

如何在 HttpURLConnection 中发送 PUT、DELETE HTTP 请求?