'HttpClient'에 해당되는 글 1건

  1. 2010.02.21 [Java] HttpClient 4.x 버전 예제 2
 
HttpClient 4.x버전으로 올라오면서 조쿰 바뀐 것 같습니다.
기록용으로 기록합니다-_-
아래 예제는.....티월드사이트의 무료사용량 조회 예제입니다-_-










[code]
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception{
        HttpClient httpclient = new DefaultHttpClient();
       
        String id = "t월드 아이디";
        String pw = "비밀번호";
       
        List<NameValuePair> qparams = new ArrayList<NameValuePair>();
        qparams.add(new BasicNameValuePair("URL", "http://www.tworld.co.kr/loginservlet.do?returnURL=http%3A%2F%2Fwww.tworld.co.kr&kind=&popup=&cmd=&reload=&ID=" + id));
        qparams.add(new BasicNameValuePair("ID", id));
        qparams.add(new BasicNameValuePair("PASSWORD", pw));
        qparams.add(new BasicNameValuePair("SERVERIP", "203.236.20.129"));
        qparams.add(new BasicNameValuePair("X", "0"));
        qparams.add(new BasicNameValuePair("Y", "0"));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(qparams, "UTF-8");
        HttpPost httpPost = new HttpPost("http://nicasams.sktelecom.com:2040/icas/fc/LogOnSV");
        httpPost.setEntity(entity);
       
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = "";
        HttpResponse response = httpclient.execute(httpPost);
        Header[] headers  = response.getAllHeaders();
        httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet();
        if (headers.length > 1){
            String url = headers[1].getValue();
            System.out.println("url = " + url);
            httpGet.setURI(new URI(url));
            responseBody = httpclient.execute(httpGet, responseHandler);
            System.out.println(responseBody);
        }
        httpGet.setURI(new URI("http://www.tworld.co.kr/normal.do?serviceId=S_BILL0070&viewId=V_CENT0261"));
        responseBody = httpclient.execute(httpGet, responseHandler);
       
        System.out.println("result = " + responseBody);
    }
}
[/code]

 
Posted by 머드초보
,