GET请求:GET请求会向服务器发索取数据的请求,从而来获取信息,该请求就像数据库的select操作一样,只是用来查询一下数据,不会修改、增加数据,不会影响资源的内容,即该请求不会产生副作用。无论进行多少次操作,结果都是一样的。
post请求:POST是向服务器端发送数据的,但是该请求会改变数据的种类等资源,就像数据库的insert操作一样,会创建新的内容。几乎目前所有的提交操作都是用POST请求的。
javaCode:
[java] view plain copy
- /**
- * 向指定URL发送GET方法的请求
- *
- */
- public static String get(String url) {
- BufferedReader in = null;
- try {
- URL realUrl = new URL(url);
- // 打开和URL之间的连接
- URLConnection connection = realUrl.openConnection();
- // 设置通用的请求属性
- connection.setRequestProperty("accept", "*/*");
- connection.setRequestProperty("connection", "Keep-Alive");
- connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
- connection.setConnectTimeout(5000);
- connection.setReadTimeout(5000);
- // 建立实际的连接
- connection.connect();
- // 定义 BufferedReader输入流来读取URL的响应 (可以设定相应的编码)
- in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
- StringBuffer sb = new StringBuffer();
- String line;
- while ((line = in.readLine()) != null) {
- sb.append(line);
- }
- return sb.toString();
- } catch (Exception e) {
- LOG.error("Exception occur when send http get request!", e);
- }
- // 使用finally块来关闭输入流
- finally {
- try {
- if (in != null) {
- in.close();
- }
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- return null;
- }
[java] view plain copy
- /**
- * 发送HttpPost请求
- *
- * @param strURL
- * 服务地址
- * @param params
- *
- * @return 成功:返回json字符串<br/>
- */
- public static String jsonPost(String strURL, Map<String, String> params) {
- try {
URL url = new URL(strURL);// 创建连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST"); // 设置请求方式
connection.setRequestProperty("Accept", "application/x-www-form-urlencoded"); // 设置接收数据的格式
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 设置发送数据的格式
connection.connect();
StringBuffer param = new StringBuffer();
for (String key : params.keySet()) {
param.append("&");
param.append(key).append("=").append(URLEncoder.encode(params.get(key), "utf-8"));
}
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
// DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写到流里面
out.writeBytes(param.toString());
//流用完记得关
out.flush();
out.close();
//获取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line,result="";
while ((line = reader.readLine()) != null){
result += line;
}
reader.close();
//该干的都干完了,记得把连接断了
connection.disconnect();
return result;
} catch (IOException e) {
e.printStackTrace();
return "error";
} - }
- /**
*
* <p>Title: doPost</p>
* <p>Description:post请求 </p>
* @param url
* @param json
* @return
*/
public static JSONObject doPost(String url,Object json){
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
JSONObject response = null;
try {
StringEntity s = new StringEntity(json.toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");//发送json数据需要设置contentType
post.setEntity(s);
HttpResponse res = client.execute(post);
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
String result = EntityUtils.toString(res.getEntity());// 返回json格式:
response = JSONObject.fromObject(result);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;
}