Tuesday, June 4, 2013

Java Create URL connection

Following is the sample for creating the URL connection when proxy is present

 Test Class

package com.java;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

//import org.apache.commons.codec.binary.Base64;

public class RClient {

      public static void main(String[] args) {
          Map proxyData = new HashMap();
          proxyData.put("pHost", "proxy.site.com");
          proxyData.put("pPort", "8080");
          proxyData.put("pUser", "USERNAME");
          proxyData.put("pPwd", "PWD");

          Map mailData = new HashMap();
          mailData.put("pQuery", "variable1=G&variable2=7120189&variable3=B");
          mailData.put("pUrl", "http://IP/sample/");

          String a = new RClient().sendPostRequest(proxyData, mailData);
      }

      /**
       * @param args :
       */
      public String sendPostRequest(Map proxyData, Map mailData) {
            String data = mailData.get("pQuery");

            try {
                  Authenticator.setDefault(new ProxyAuthenticator(proxyData.get("pUser"), proxyData.get("pPwd")));
                  System.setProperty("http.proxyHost", proxyData.get("pHost"));
                  System.setProperty("http.proxyPort", proxyData.get("pPort"));
                 
                  URL url = new URL(mailData.get("pUrl"));
                  HttpURLConnection con = (HttpURLConnection) url.openConnection();
                  con.setRequestMethod("POST");
                  con.setDoOutput(true);
                  con.setDoInput(true);

                  OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
                 
                  //write parameters
                  writer.write(data);
                  writer.flush();

                  if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {//response code is 200
                        return "OK";
                  } else {
                      return "ERROR: "+ con.getResponseCode();
                  }

            } catch (Exception e) {
                  return "ERROR: "+ e.getMessage();
            }
      }
     
}


================================================
Proxy authenticator class     
     
package com.java;
import java.net.Authenticator;
import java.net.PasswordAuthentication;

class ProxyAuthenticator extends Authenticator {

    private String user, password;

    public ProxyAuthenticator(String user, String password) {
        this.user = user;
        this.password = password;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password.toCharArray());
    }
}


No comments:

This site providing you to JAVA Language easy way. I think if you are beginner to JAVA refer this to implements you knowledge using only with very simplest codes.