GET/POST Requst to REST API using Spring Boot

前端 未结 4 1236
北海茫月
北海茫月 2021-01-07 08:01

I have a REST Service an external server like https://api.myrestservice.com and I have a Spring Boot Application running locally on http://localhost:8080<

4条回答
  •  长情又很酷
    2021-01-07 08:18

    There are many ways to do it. Like Apache HTTP Components and other. Sample

    String type = "application/x-www-form-urlencoded" Or Set your desire content type;
    String encodedData = URLEncoder.encode( rawData, "UTF-8" ); 
    URL u = new URL("your remote url");
    HttpURLConnection conn = (HttpURLConnection) u.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty( "Content-Type", type );
    conn.setRequestProperty( "Content-Length", 
    String.valueOf(encodedData.length()));
    OutputStream os = conn.getOutputStream();
    os.write(encodedData.getBytes());
    

    There are a couple of thing going on here, Like URLEncoding is really mattered when came to security. Note: Source of above code:here.

提交回复
热议问题