java 로 만든 응용프로그램에서 http 요청으로 데이터 를 처리 해야하는경우
아래 코드는 샘플 코드이다.
쿠키나 토큰등 은 피들러로 캡쳐해서 셋팅하는걸 추천한다.
package com.app.fns.metketRequester;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.StringJoiner;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
App app = new App();
app.looper();
//app.doPost();
}
public String doPost(String token) {
URL url = null;
HttpURLConnection conn = null;
String next = "";
try {
System.out.println("token : " + token);
url = new URL("대상 URL");
String param = "od_action=buy&od_pay_status=REQ&od_market_price=5145000&od_total_cost=160845.048&od_temp_coin=0.03120000&channel=krw_btc¤cy=btc&token=" + token;
byte[] postDataBytes = param.toString().getBytes("UTF-8");
conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST"); // GET 요청일 경우 변경
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.addRequestProperty("Connection", "keep-alive");
conn.addRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
conn.addRequestProperty("Origin", "http://bittrade.funhansoft.local");
conn.addRequestProperty("X-Requested-With", "XMLHttpRequest");
conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");
conn.addRequestProperty("Content-Type", " application/x-www-form-urlencoded; charset=UTF-8");
conn.addRequestProperty("Referer", "http://bittrade.funhansoft.local/trade/order");
conn.addRequestProperty("Accept-Encoding", "gzip, deflate");
conn.addRequestProperty("Accept-Language", "ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7,ja;q=0.6");
conn.addRequestProperty("Cookie", "f484570d7cf557020e11ace406901b10=YmxhY2stdGhlbWU%3D; 4994a8ffeba4ac3140beb89e8d41f174=a28%3D; Language=a28%3D; theme=d2hpdGUtdGhlbWU%3D; 817eebc5b49c13f8e6a0a7d159a49c09=ZnVuaGFuc29mdC5jb20%3D; c26e8178126688deb863604bef4b0cda=TUIxOS02ZTcxNGI3YjRkOWFjNmQ1ZWE3ZGU3OTk2OTFkYzk1OQ%3D%3D; PHPSESSID=pil4m4ostqga1dt6fnfhc01smn");
conn.addRequestProperty("DNT", "1");
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
for (int c; (c = in.read()) >= 0;)
sb.append((char)c);
String response = sb.toString();
System.out.println(response);
JSONParser parser = new JSONParser();
Object obj = parser.parse( response );
JSONObject jsonObj = (JSONObject) obj;
next = (String) jsonObj.get("token");
}catch(Exception e) {
e.printStackTrace();
}finally {
if(conn != null) {
conn.disconnect();
}
}
return next;
}
//반복적으로 보내기 위한 테스트
public void looper() {
System.out.println("looper()");
String token = "pil4m4ostqga1dt6fnfhc01smn1548932147";
int count = 10;
for(int i =0; i < count; i++) {
if(token.equals("")) {
System.out.println("error");
break;
}
token = doPost(token);
}
}
}
댓글 없음:
댓글 쓰기