0

I'm new user on the site web. My problem is the follow:

I've a java project in Eclipse tool. Really, it is a java server that must parse data in a web and after simulate login in the same web. I active this server with android phone (client) that send a message (tipical client-server app). All this done with local wifi connection. The problem is:

Exception in thread "main" java.net.UnknownHostException: larrun.iberdrola.es
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.followRedirect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
at HttpIberdrola.sendPost(HttpIberdrola.java:91)
at HttpIberdrola.main(HttpIberdrola.java:54)  

and code is:

    public class HttpIberdrola {

    private List<String> cookies;
    private HttpsURLConnection conn;

    private final String USER_AGENT = "Mozilla/5.0";

    public static void main(String[] args) throws Exception {


ServerSocket sk = new ServerSocket(80);  
Socket cliente = sk.accept();
    BufferedReader entrada = new BufferedReader(new    InputStreamReader(cliente.getInputStream()));
    PrintWriter salida = new PrintWriter(new OutputStreamWriter(cliente.getOutputStream()),true);
    String datos = entrada.readLine(); //RECIBE ENVÍO CONEXION DEL CLIENTE
    salida.println(datos); //ENVIA CONFIRMACION CONEXION A CLIENTE
    cliente.close();


String url = "https://www.iberdrola.es/clientes/index";
String iberdrola = "https://www.iberdrola.es/02sica/clientesovc/iberdrola?IDPAG=ESOVC_CONTRATOS_LCE";

HttpIberdrola http = new HttpIberdrola();

// make sure cookies is turn on
CookieHandler.setDefault(new CookieManager());

// 1. Send a "GET" request, so that you can extract the form's data.
String page = http.GetPageContent(url);
String postParams = http.getFormParams(page, "USER", "PASS");

// 2. Construct above post's content and then send a POST request for
// authentication
http.sendPost(url, postParams);

// 3. success then go to gmail.
String result = http.GetPageContent(iberdrola);
System.out.println(result);
    }

    private void sendPost(String url, String postParams) throws Exception {

URL obj = new URL(url);
conn = (HttpsURLConnection) obj.openConnection();

// Acts like a browser
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Host", "www.iberdrola.es");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept",
    "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
conn.setRequestProperty("Accept-Language", "es-ES,es;q=0.8");
for (String cookie : this.cookies) {
    conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
}
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Referer", "https://www.iberdrola.es/02sica/ngc/es/util/desconectar.jsp");
conn.setRequestProperty("Content-Type", "text/html;charset=ISO-8859-1");
conn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));

conn.setDoOutput(true);
conn.setDoInput(true);

// Send post request
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(postParams);
wr.flush();
wr.close();

int responseCode = conn.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + postParams);
System.out.println("Response Code : " + responseCode);

BufferedReader in = 
         new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}
in.close();
//System.out.println(response.toString());

    }

    private String GetPageContent(String url) throws Exception {

URL obj = new URL(url);
conn = (HttpsURLConnection) obj.openConnection();

// default is GET
conn.setRequestMethod("GET");

conn.setUseCaches(false);

// act like a browser
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept",
    "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
conn.setRequestProperty("Accept-Language", "es-ES,es;q=0.8");
if (cookies != null) {
    for (String cookie : this.cookies) {
        conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
    }
}
int responseCode = conn.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);

BufferedReader in = 
        new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}
in.close();

// Get the response cookies
setCookies(conn.getHeaderFields().get("Set-Cookie"));

return response.toString();

    }

    public String getFormParams(String html, String username, String password)
    throws UnsupportedEncodingException {

System.out.println("Extracting form's data...");

Document doc = Jsoup.parse(html);

// Iberdrola form id
Element loginform = doc.getElementById("login");
Elements inputElements = loginform.getElementsByTag("input");
List<String> paramList = new ArrayList<String>();
for (Element inputElement : inputElements) {
    String key = inputElement.attr("name");
    String value = inputElement.attr("value");

    if (key.equals("alias"))
        value = username;
    else if (key.equals("clave"))
        value = password;
    paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
}

// build parameters list
StringBuilder result = new StringBuilder();
for (String param : paramList) {
    if (result.length() == 0) {
        result.append(param);
    } else {
        result.append("&" + param);
    }
}

return result.toString();
    }

I've read other posts with similar problems (proxy, firewall,...) but I did not find answer to my problem. I've tried the same example with the login account of Google email and his "id forms" page and works perfect. I've disabled the windows 7 firewall. I can do ping www.google.com (receive packages) but cannot do ping www.iberdrola.es (all packages missed).

I think that the problem is that iberdrola host is protected, Am I confused? The problem is that I need(obligatory) to get the data on this page and access it. What I can I do to solve this?

Thanks in advance

byturbin
  • 1
  • 1
  • 2

1 Answers1

0

The host name larrun.iberdrola.es is not defined in DNS. That hostname does not exist.

www.iberdrola.es exists and is reachable on port 80 (WWW). Whether or not a host responds to ping will depend on how the host and/or the domain's firewall is configured. Some hosts respond to ping, others do not. Lack of a response to ping does not mean the host is not reachable via TCP.

However, in your case, the hostname you gave does not exist. You will need to find out the correct hostname and substitute that.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • Hi and thank you to comment. I'm using port 80 and the hostname is called www.iberdrola.es . That can be seen in some pages of ipaddress information or in the iberdrola web using "inspect element". I observed this... for this reason i wrote this post for help. – byturbin Sep 30 '13 at 21:33
  • `Exception in thread "main" java.net.UnknownHostException: larrun.iberdrola.es` -- This does not match your code. Please correct your post so the exception is the one seen when running your code. – Jim Garrison Sep 30 '13 at 22:14
  • This exception appears when it executes **http.sendPost(url, postParams);** function (java:54) .Then, inside of him, when it executes **int responseCode = conn.getResponseCode();** sentence (java:91). **larrun.iberdrola.es** doesn't appear in code and isn't hostname. This only appear in exception, but I don't know the reason – byturbin Oct 01 '13 at 09:47