Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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 从aspx获取内容的HttpUrlConnection?_Java_Httpurlconnection - Fatal编程技术网

Java 从aspx获取内容的HttpUrlConnection?

Java 从aspx获取内容的HttpUrlConnection?,java,httpurlconnection,Java,Httpurlconnection,所以,我想从aspx页面获取数据。我需要执行以下步骤 1) 发送GET请求和GET _VIEWSTATE以及_EVENTVALIDATION。 2) 发送带有参数的POST请求(登录),然后更新_VIEWSTATE和_EVENTVALIDATION 3) 发送带有参数的POST请求(选择字段),然后更新变量 4) 发送带有参数的POST请求(按下按钮) 5) 解析页面内容。 在脚本之后动态加载页面内容。 我尝试使用HttpUrlConnection public String sendGet(

所以,我想从aspx页面获取数据。我需要执行以下步骤

1) 发送GET请求和GET _VIEWSTATE以及_EVENTVALIDATION。 2) 发送带有参数的POST请求(登录),然后更新_VIEWSTATE和_EVENTVALIDATION 3) 发送带有参数的POST请求(选择字段),然后更新变量 4) 发送带有参数的POST请求(按下按钮) 5) 解析页面内容。 在脚本之后动态加载页面内容。 我尝试使用HttpUrlConnection

 public String sendGet(String url) throws Exception {
        StringBuilder result = new StringBuilder();
        URL ur = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) ur.openConnection();
        connection.setRequestMethod("GET");
        connection.setUseCaches(false);
        connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36");
        connection.setRequestProperty("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        while ((inputLine = br.readLine()) != null) {
            result.append(inputLine);
        }
        br.close();

        return result.toString();
    }
 public String sendPost(String url, List<NameValuePair> formParams) throws Exception {
        URL ur = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) ur.openConnection();
        connection.setReadTimeout(10000);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setConnectTimeout(10000);
        connection.setRequestMethod("POST");
        for (HttpCookie cookie : this.cookies) {
            connection.addRequestProperty("Cookie", cookie.toString());
        }
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestProperty("Content-Language", "en-US");
        connection.connect();
        OutputStream os = connection.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getQuery(formParams));
        writer.flush();
        writer.close();
        os.close();
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String response = "";
        String line;
        while ((line = br.readLine()) != null) {
            response += line;
        }
        System.out.println(Jsoup.parse(response).text());
        updateViewState(response);
        updateSubSession(response);
        return response;
    }
我认为我得到曲奇是错误的,我试着得到这样的曲奇:

connection.getHeaderFields().get("Set-Cookie");  
但是
Cookie
null

我哪里错了?多谢各位

这篇文章真的应该被标记。你只会得到基于意见的回应。我会试着把你的问题说得更具体一点。也包括一些代码,这样我们就可以看到你在看什么了。我试着用httpclient来做这件事。但结果是一样的。所以也许这是不可能的?)这篇文章真的应该被标记。你只会得到基于意见的回应。我会试着把你的问题说得更具体一点。也包括一些代码,这样我们就可以看到你在看什么了。我试着用httpclient来做这件事。但结果是一样的。那么也许这是不可能的?)
public String sendPost(String url, List<NameValuePair> formParams) throws Exception {
        URL ur = new URL(url+subSession);
        HttpURLConnection connection = (HttpURLConnection) ur.openConnection();
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: http://www.aogc2.state.ar.us:8080/DWClient/Login.aspx?DWSubSession=9852&v=1589
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
    at com.company.UrlConnection.sendPost(UrlConnection.java:111)
    at com.company.UrlConnection.login(UrlConnection.java:138)
    at com.company.UrlConnection.start(UrlConnection.java:35)
    at com.company.Main.main(Main.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at 
connection.getHeaderFields().get("Set-Cookie");