Http Request 하기






JAVA에서  HttpURLConnection 함수로 Http 연결이 가능합니다.

기본적으로 GET 방식과 POST 방식 둘다 가능하지만 코드는 "GET" 방식만 넣어놨습니다.


GET 방식이기 때문에 URL에 Parameter 를 추가하여 전송하면 됩니다.


예) http://127.0.0.1:8080/send?key=데이터


private final String USER_AGENT = "Mozilla/5.0";

public String sendHttp(String url) throws Exception {
        
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    con.setRequestProperty("User-Agent", USER_AGENT);
    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

Share this

Related Posts

:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
:-?
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
$-)
(y)
(f)
x-)
(k)
(h)
cheer