티스토리 뷰

Java Post 호출 클래스


post 호출을 할 때 파라메터를 넘기면서 호출할 일이 있어서 만들어봄


https://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/ 참고함


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@Slf4j
public class PostGetter {
    // HTTP POST request
    public String sendPost(String url, String urlParameters) throws Exception {
 
 
        URL obj = new URL(url);
        HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
 
        //add reuqest header
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", USER_AGENT);
        con.setRequestProperty("Accept-Language""en-US,en;q=0.5");
 
 
 
        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();
 
        int responseCode = con.getResponseCode();
        log.info("\nSending 'POST' request to URL : " + url);
        log.info("Post parameters : " + urlParameters);
        log.info("Response Code : " + responseCode);
 
        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
 
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
 
        return response.toString();
 
    }
}
 
cs

호출 할 때는
1
2
3
4
5
6
7
@Test
    public void name() throws Exception {
        String url = "https://livebetting.bwin.com/en/live/secure/api/betoffer/event";
        String urlParameters = "id=6658927&full=true";
        String responseString = postGetter.sendPost(url, urlParameters);
        System.out.println(responseString);
    }

cs


위와 같이 호출함


파라메터가 2개임 id하고 full 이렇게 두개이고 &으로 연결함


end.




공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함