package com.cplatform.movie.back.test;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class HttpURLConnectionTest {
public static final String GET_URL = "http://112.4.27.9/mall-back/if_user/store_list?storeId=32";
public static final String POST_URL = "http://112.4.27.9/mall-back/if_user/store_list";
public static void httpURLConectionGET() {
try {
URL url = new URL(GET_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
connection.disconnect();
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
System.out.println("失败!");
}
}
public static void httpURLConnectionPOST () {
try {
URL url = new URL(POST_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.connect();
DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
String parm = "storeId=" + URLEncoder.encode("32", "utf-8");
dataout.writeBytes(parm);
dataout.flush();
dataout.close();
System.out.println(connection.getResponseCode());
BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();
while ((line = bf.readLine()) != null) {
sb.append(bf.readLine());
}
bf.close();
connection.disconnect();
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
httpURLConnectionPOST();
}
}