发布到带有cookie的asp

时间:2011-07-21 18:25:17

标签: java asp.net cookies post session-cookies

我正在尝试发布到asp并获得返回页面。该页面启动一个会话并向我发送一个cookie。如何在请求中将其发回?

    URL oracle = new URL("http://taxinquiry.princegeorgescountymd.gov/taxsummary.aspx");
    HttpURLConnection yc = (HttpURLConnection) oracle.openConnection();
    yc.setDoOutput(true);
    yc.setDoInput(true);
    yc.setAllowUserInteraction(true);
    yc.setRequestMethod("HEAD");

    String cookie = yc.getHeaderField(5);

    yc.disconnect();

    HttpURLConnection yd = (HttpURLConnection) oracle.openConnection();

    yd.setRequestMethod("POST");
    yd.setRequestProperty("Content-Type", "text/html; charset=utf-8");
    yd.setRequestProperty("Content-Length", "19004");
    yd.setRequestProperty("Cache-Control", "private");
    yd.setRequestProperty("Set-Cookie", cookie);
    yd.setRequestProperty("X-AspNet-Version", "1.1.4322");
    yd.setRequestProperty("X-Powered-By", "ASP.NET");
    yd.setRequestProperty("Server", "Microsoft-IIS/6.0");
    yd.setDoOutput(true);
    HttpURLConnection.setFollowRedirects(true);

    OutputStreamWriter out = new OutputStreamWriter(yd.getOutputStream());

    out.write(data);
    out.flush();
    //out.write(data);
    out.close();

    BufferedReader in = new BufferedReader(new InputStreamReader(yd.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null) 
        System.out.println(inputLine);
    in.close();

}

}

我想我发现你不能在相同的连接中读写,但是你不会丢失服务器刚刚发送的cookie吗?任何澄清都会有所帮助。

问候。

1 个答案:

答案 0 :(得分:0)

yd.setRequestProperty("Cookie", "name=John;surname=Lennon;jsessionid=1234567890");