HttpURLConnection 액세스 서버 인터페이스

3744 단어
1, 클라이언트 요청 도구 클래스
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

/** 
 * Http  
 * 
 */
public class HttpRequestUtil {

  public String submitPostData(String strUrlPath, Map params) {
    String postData = getRequestData(params, "utf-8").toString();//  
    try {
      //  POST 
      URL url = new URL(strUrlPath);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
      conn.setRequestMethod("POST");
      // conn.setRequestProperty("Content-Type", "application/json");
      conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      conn.setRequestProperty("Connection", "Keep-Alive");
      conn.setUseCaches(false);
      conn.setDoOutput(true);
      conn.setRequestProperty("Content-Length", "" + postData.length());
      OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
      out.write(postData);
      out.flush();
      out.close();
      //  
      if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
        System.out.println("connect failed!" + conn.getResponseCode());
        return "";
      }
      //  
      String line, result = "";
      BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
      while ((line = in.readLine()) != null) {
        result += line + "
"; } in.close(); return result; } catch (IOException e) { e.printStackTrace(System.out); } return ""; } /* * Function : Param : params ,encode */ public static StringBuffer getRequestData(Map params, String encode) { StringBuffer stringBuffer = new StringBuffer(); // try { for (Map.Entry entry : params.entrySet()) { stringBuffer.append(entry.getKey()).append("=").append(entry.getValue()).append("&"); } stringBuffer.deleteCharAt(stringBuffer.length() - 1); // "&" } catch (Exception e) { e.printStackTrace(); } return stringBuffer; } }

2, 구체적 요청
      HttpRequestUtil util = new HttpRequestUtil();
      String url = " ";
      //map 
      Map paramMap = new HashMap<>();
      paramMap.put("1", 1);
      paramMap.put("2", 2);
      paramMap.put("3", 3);
      util.submitPostData(url, paramMap);

3, 서버 측 인터페이스
@RequestMapping(value = "saveData", method = RequestMethod.POST)
  @ResponseBody
  public ErrorObject saveData(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // 
    String roomStr = request.getParameter("1");
    String areaStr = request.getParameter("2");
    String dictionaryStr = request.getParameter("3");
    object object= JSON.parseObject(1, object.class);
    return tblRoomsService.save(object);
    // 
    // request.setCharacterEncoding("UTF-8");
    // BufferedReader br = new BufferedReader(new
    // InputStreamReader(request.getInputStream(), "utf-8"));
    // String line = "";
    // String paramStr = "";
    // while (null != (line = br.readLine())) {
    // paramStr += line;
    // }

  }

좋은 웹페이지 즐겨찾기