import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
//import java.net.MalformedURLException;
import java.net.URL;
//import java.net.URLEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
//import com.alibaba.druid.support.logging.Log;
public class HttpURLConnect {
@Autowired
// 修改
@RequestMapping(value = "put/{method}", method = RequestMethod.PUT)
public @ResponseBody String put(@PathVariable String mothed) {
return "put:" + mothed;
}
// 新增
@RequestMapping(value = "post/{method}", method = RequestMethod.POST)
public @ResponseBody String post(@PathVariable String mothed, String id, String name) {
System.out.println("id:" + id);
System.out.println("name:" + name);
return "post:" + mothed;
}
// 刪除
@RequestMapping(value = "delete/{method}", method = RequestMethod.DELETE)
public @ResponseBody String delete(@PathVariable String mothed) {
return "delete:" + mothed;
}
// 查找
@RequestMapping(value = "get/{method}", method = RequestMethod.GET)
public @ResponseBody String get(@PathVariable String mothed) {
return "get:" + mothed;
}
// HttpURLConnection 調用Restful接口
@RequestMapping(value = "dealCon/{method}")
public @ResponseBody String dealCon(@PathVariable String url, String param, String session) throws IOException {
StringBuilder sb = new StringBuilder();
URL restServiceURL = new URL(url);
HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
httpConnection.setRequestProperty("Cookie", session);
// method 輸入小寫,轉換成 GET POST DELETE PUT
// httpConnection.setRequestProperty("Accept", "application/json");
if (param == null) {
httpConnection.connect();
}
else {
// 打開輸出開關
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
// httpConnection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
httpConnection.setRequestProperty("Accept", "application/json, text/plain, */*");
httpConnection.setDoOutput(true);
httpConnection.connect();
// 傳遞參數,獲取HttpURLConnection對象的輸出流
OutputStream outputStream = httpConnection.getOutputStream();
//請求參數發送
outputStream.write(param.getBytes());
// flush輸出流的緩沖
outputStream.flush();
}
if (httpConnection.getResponseCode() != 200) {
throw new RuntimeException("HTTP GET Request Failed with Error code : " + httpConnection.getResponseCode());
}
//定義BufferedReader輸入流來讀取URL的響應
BufferedReader responseBuffer = new BufferedReader(new InputStreamReader((httpConnection.getInputStream())));
System.out.println("Output from Server: \n");
String output;
while ((output = responseBuffer.readLine()) != null) {
System.out.println(output);
sb.append(output);
System.out.println("=============");
}
httpConnection.disconnect();
return sb.toString();
}
/**
* post方式登錄
*
* @param username
* @param password
* @param loginAction
* @return
* @throws Exception
*/
public String getCookie(String urlLogin, String accountInfo) throws Exception {
// 登錄
URL url = new URL(urlLogin);
// String param = "username="+username+"&password="+password;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStream out = conn.getOutputStream();
out.write(accountInfo.getBytes());
out.flush();
out.close();
String sessionId = "";
String cookieVal = "";
String key = null;
// 取cookie
for (int i = 1; (key = conn.getHeaderFieldKey(i)) != null; i++) {
if (key.equalsIgnoreCase("Set-Cookie")) {
cookieVal = conn.getHeaderField(i);
cookieVal = cookieVal.substring(0, cookieVal.indexOf(";"));
sessionId = sessionId + cookieVal + ";";
System.out.println("sessionId======" + sessionId);
}
}
return sessionId;
}
}