Java GET和POST请求

Posted 不当咸鱼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java GET和POST请求相关的知识,希望对你有一定的参考价值。

     从表面来看GET和POST请求:

    GET请求是在url后直接附上请求体,url和请求体之间用"?"分割,不同参数之间用"&"分隔,%XX中的XX为该符号以16进制表示的ASCII,如果数据是英文字母/数字,原样发送,如果是空格,转换为+,如果是中文/其他字符,则直接把字符串用BASE64加密。

    POST把提交的数据则放置在是HTTP包的包体中,Post没有限制提交的数据。Post比Get安全,当数据是中文或者不敏感的数据,则用get,因为使用get,参数会显示在地址,对于敏感数据和不是中文字符的数据,则用post

Java中get请求:

package com.httpGetTest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class HttpGetTest {

	public static void main(String[] args) {

		CreatHttpUrl creatUrl = new CreatHttpUrl();
	    String url = CreatHttpUrl.creatUrl("teacher", "en", "zh-CHS");
	    //System.out .print(url);
	    
	   // new readByGet().start();
	    readData();
	}
	public static void readData()
	{
		try { 
	    	CreatHttpUrl creatUrl = new CreatHttpUrl();
		    String urlStr = CreatHttpUrl.creatUrl("teacher", "en", "zh-CHS");
			URL url = new URL(urlStr);
			URLConnection connection = url.openConnection();
			InputStream is = connection.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader bf = new BufferedReader(isr);
			
			String line;
			StringBuilder builder = new StringBuilder();
			while((line = bf.readLine()) != null)
			{
				builder.append(line);
			}
			
			bf.close();
			isr.close();
			is.close();
			
			System.out.println(builder);
			
	    } catch (Exception e) {
			e.printStackTrace();
		}

	}
}

 

Java中的post请求:

package com.httpPostTest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class HttpPostTest {

	public static void main(String[] args) {

		CreatHttpUrl creatUrl = new CreatHttpUrl();
		String urlStr = "https://openapi.youdao.com/api";
		String paramStr = creatUrl.createParam("good","en","zh-CHS");
		
		System.out.println(paramStr);
		
		try {
			//设置请求相关设置
			URL url = new URL(urlStr);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestProperty("encoding", "UTF-8");
			connection.setRequestMethod("POST");
			connection.setDoInput(true);
			connection.setDoOutput(true);
			
			//post提交数据
			OutputStream os = connection.getOutputStream();
			OutputStreamWriter osw = new OutputStreamWriter(os);
			BufferedWriter writer = new BufferedWriter(osw);
			
			writer.write(paramStr);
			writer.flush();
			
			//读取获取的数据
			InputStream is = connection.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			
			String line;
			StringBuilder builder = new StringBuilder();
			while((line = br.readLine()) != null)
			{
				builder.append(line);
			}
			
			writer.close();
			osw.close();
			os.close();
			br.close();
			isr.close();
			is.close();
			
			
			System.out.print(builder.toString());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 



以上是关于Java GET和POST请求的主要内容,如果未能解决你的问题,请参考以下文章

转:PHP中的使用curl发送请求(GET请求和POST请求)

Java学习心得之 HttpClient的GET和POST请求

Java GET和POST请求

Java实现post和get请求

java 用Java发送GET和POST请求

java http请求get与post