HTTP POST 여러개의 파라미터를 포함한 request 테스트

 

아파치 httpclient 라이브러리를 이용하여 POST 형태로 다중 파라메터를 전송하는 예제입니다.

 

 

 

 

package com.redjava.interfaces.https;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

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

public class httpClientParams {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
    HttpClient client = new DefaultHttpClient();
      HttpPost post = new HttpPost("https://www.google.com/accounts/ClientLogin");

      try {

        List<namevaluepair> nameValuePairs = new ArrayList<namevaluepair>(1);
        nameValuePairs.add(new BasicNameValuePair("Email", "jsaclova"));
        nameValuePairs
            .add(new BasicNameValuePair("Passwd", "************"));
        nameValuePairs.add(new BasicNameValuePair("accountType", "GOOGLE"));
        nameValuePairs.add(new BasicNameValuePair("source", "Google-cURL-Example"));
        nameValuePairs.add(new BasicNameValuePair("service", "ac2dm"));

        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        String line = "";
        while ((line = rd.readLine()) != null) {
          System.out.println(line);
          if (line.startsWith("Auth=")) {
            String key = line.substring(5);
            // Do something with the key
          }

        }
      } catch (IOException e) {
        e.printStackTrace();
      }
   }


}
</namevaluepair></namevaluepair>

블로그 이미지

슬픈외로움

개발이 어려워? 모든것엔 답이있다...

,

HTTP POST 여러개의 파라미터를 포함한 request 테스트

 

아파치 httpclient 라이브러리를 이용하여 POST 방식으로 페이지를 호출하는 예제입니다.

 

 

package com.redjava.interfaces.https;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

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

public class httpClientParams {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		  HttpClient client = new DefaultHttpClient();
		    HttpPost post = new HttpPost("https://www.google.com/accounts/ClientLogin");

		    try {

		      List<namevaluepair> nameValuePairs = new ArrayList<namevaluepair>(1);
		      nameValuePairs.add(new BasicNameValuePair("Email", "jsaclova"));
		      nameValuePairs
		          .add(new BasicNameValuePair("Passwd", "************"));
		      nameValuePairs.add(new BasicNameValuePair("accountType", "GOOGLE"));
		      nameValuePairs.add(new BasicNameValuePair("source", "Google-cURL-Example"));
		      nameValuePairs.add(new BasicNameValuePair("service", "ac2dm"));

		      post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		      HttpResponse response = client.execute(post);
		      BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

		      String line = "";
		      while ((line = rd.readLine()) != null) {
		        System.out.println(line);
		        if (line.startsWith("Auth=")) {
		          String key = line.substring(5);
		          // Do something with the key
		        }

		      }
		    } catch (IOException e) {
		      e.printStackTrace();
		    }
	  }


}
블로그 이미지

슬픈외로움

개발이 어려워? 모든것엔 답이있다...

,