Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Eclipse中带有openConnection()的UnknownHostException和未知源_Java_Eclipse_Connection_Httpurlconnection_Unknown Host - Fatal编程技术网

Java Eclipse中带有openConnection()的UnknownHostException和未知源

Java Eclipse中带有openConnection()的UnknownHostException和未知源,java,eclipse,connection,httpurlconnection,unknown-host,Java,Eclipse,Connection,Httpurlconnection,Unknown Host,我是网站的新用户。我的问题如下: 我在Eclipse工具中有一个java项目。实际上,它是一个java服务器,必须解析web中的数据,然后在同一web中模拟登录。我使用发送消息的android手机(客户端)激活此服务器(tipical客户端服务器应用程序)。所有这些都是通过本地wifi连接完成的。问题是: Exception in thread "main" java.net.UnknownHostException: larrun.iberdrola.es at java.net.Abstra

我是网站的新用户。我的问题如下:

我在Eclipse工具中有一个java项目。实际上,它是一个java服务器,必须解析web中的数据,然后在同一web中模拟登录。我使用发送消息的android手机(客户端)激活此服务器(tipical客户端服务器应用程序)。所有这些都是通过本地wifi连接完成的。问题是:

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)  
线程“main”java.net.UnknownHostException中的异常:larrun.iberdrola.es 位于java.net.AbstractPlainSocketImpl.connect(未知源) 位于java.net.PlainSocketImpl.connect(未知源) 位于java.net.socksocketimpl.connect(未知源) 位于java.net.Socket.connect(未知源) 位于sun.security.ssl.SSLSocketImpl.connect(未知源) 位于sun.security.ssl.BaseSSLSocketImpl.connect(未知源) 位于sun.net.NetworkClient.doConnect(未知源) 位于sun.net.www.http.HttpClient.openServer(未知来源) 位于sun.net.www.http.HttpClient.openServer(未知来源) 位于sun.net.www.protocol.https.HttpsClient。(未知来源) 位于sun.net.www.protocol.https.HttpsClient.New(未知来源) 位于sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(未知源) 位于sun.net.www.protocol.http.HttpURLConnection.plainConnect(未知源) 位于sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(未知源) 位于sun.net.www.protocol.http.HttpURLConnection.followRedirect(未知源) 位于sun.net.www.protocol.http.HttpURLConnection.getInputStream(未知源) 位于java.net.HttpURLConnection.getResponseCode(未知源) 位于sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(未知源) 在HttpIberdrola.sendPost(HttpIberdrola.java:91) 位于HttpIberdrola.main(HttpIberdrola.java:54) 代码是:

    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();
    }
公共类HttpIberdrola{
私有列表cookies;
专用HttpsURLConnection连接器;
私有最终字符串用户_AGENT=“Mozilla/5.0”;
公共静态void main(字符串[]args)引发异常{
ServerSocket sk=新的ServerSocket(80);
Socket cliente=sk.accept();
BufferedReader entrada=新的BufferedReader(新的InputStreamReader(client.getInputStream());
PrintWriter salida=new PrintWriter(new OutputStreamWriter(client.getOutputStream()),true);
字符串datos=entrada.readLine();//RECIBE ENVÍO CONEXION DEL CLIENTE
salida.println(datos);//客户确认环境
cliente.close();
字符串url=”https://www.iberdrola.es/clientes/index";
字符串iberdrola=”https://www.iberdrola.es/02sica/clientesovc/iberdrola?IDPAG=ESOVC_CONTRATOS_LCE";
HttpIberdrola http=新的HttpIberdrola();
//确保cookies已打开
setDefault(新的CookieManager());
//1.发送“获取”请求,以便提取表单数据。
String page=http.GetPageContent(url);
字符串postParams=http.getFormParams(第页,“用户”,“传递”);
//2.构建上述帖子的内容,然后发送帖子请求
//认证
http.sendPost(url,postParams);
//3.成功,然后转到gmail。
字符串结果=http.GetPageContent(iberdrola);
系统输出打印项次(结果);
}
私有void sendPost(字符串url、字符串后参数)引发异常{
URL obj=新URL(URL);
conn=(HttpsURLConnection)obj.openConnection();
//就像一个浏览器
conn.SETUSECHACHES(假);
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“主机”,“www.iberdrola.es”);
conn.setRequestProperty(“用户代理”,用户代理);
conn.setRequestProperty(“接受”,
“text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8”);
conn.setRequestProperty(“接受语言”,“es,es;q=0.8”);
for(字符串cookie:this.cookies){
conn.addRequestProperty(“Cookie”,Cookie.split(“;”,1)[0]);
}
conn.setRequestProperty(“连接”、“保持活动”);
conn.setRequestProperty(“Referer”https://www.iberdrola.es/02sica/ngc/es/util/desconectar.jsp");
conn.setRequestProperty(“内容类型”,“文本/html;字符集=ISO-8859-1”);
conn.setRequestProperty(“内容长度”,Integer.toString(postParams.Length());
连接设置输出(真);
conn.setDoInput(真);
//发送邮寄请求
DataOutputStream wr=新的DataOutputStream(conn.getOutputStream());
wr.writeBytes(后参数);
wr.flush();
wr.close();
int responseCode=conn.getResponseCode();
System.out.println(“\n向URL发送'POST'请求:“+URL”);
System.out.println(“Post参数:“+postParams”);
System.out.println(“响应代码:“+responseCode”);
BufferedReader in=
新的BufferedReader(新的InputStreamReader(conn.getInputStream());
字符串输入线;
StringBuffer响应=新的StringBuffer();
而((inputLine=in.readLine())!=null){
追加(inputLine);
}
in.close();
//System.out.println(response.toString());
}
私有字符串GetPageContent(字符串url)引发异常{
URL obj=新URL(URL);
conn=(HttpsURLConnection)obj.openConnection();
//默认值是GET
conn.setRequestMethod(“GET”);
conn.SETUSECHACHES(假);
//像一个浏览器
conn.setRequestProperty(“用户代理”,用户代理);
conn.setRequestProperty(“接受”,
“text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8”);
conn.setRequestProperty(“接受语言”,“es,es;q=0.8”);
如果(cookies!=null){
for(字符串cookie:this.cookies){
conn.addRequestProperty(“Cookie”,Cookie.split(“;”,1)[0]);
}
}
int responseCode=conn.getResponseCode();
System.out.println(“\n向URL发送'GET'请求:“+URL”);
System.out.println(“响应代码:“+responseCode”);
BufferedReader in=
新的BufferedReader(新的InputStreamReader(conn.getInputStream());
字符串输入线;
StringBuffer响应=新的StringBuffer();
而((inputLine=in.readLine())!=null){
追加(inputLine);
}
in.close();
//获取响应cookies
setCookies(conn.getHeaderFields().get(“setcookie”);
返回response.toString();
}
公共字符串getFormParams(字符串html、字符串用户名、字符串密码)
投不支持票