0

我有一个 URLConnection,它可以访问一个网页。

URL url = new URL("https://domain");
   con = url.openConnection();
   con.setDoOutput(true);

然后我使用发送一些数据到服务器con.setRequestProperty()

我使用 ma 指定字段获取响应 cookie

String headerValue = con.getHeaderField(6);

我还得到了 html 并从那里解析了一个图像 url。但这里有一个问题。当我访问我的图像时,我只能通过将缓存数据发送回服务器来获取此图像。

所以我打开一个新连接

URL url1 = new URL("https://domain/image); 
    URLConnection con1 = url1.openConnection();

我将 cookie 发送回服务器con1.setRequestProperty("Cookie", headerValue);

最后我尝试使用 BufferedInputStream 访问图像,然后在 JLabel 中创建一个 iamge

BufferedInputStream in = new BufferedInputStream(con1.getInputStream());
       ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
       int c;
       while ((c = in.read()) != -1) {
         byteArrayOut.write(c);
       }


       Image image = Toolkit.getDefaultToolkit().createImage(
               byteArrayOut.toByteArray());

    label.setIcon(new ImageIcon(image));

问题是这似乎不起作用。是通过 URlConnection 从服务器获取文件的另一种方式吗?错误代码

Server returned HTTP response code: 400 for URL: https://domain/image
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)

提前致谢

4

1 回答 1

0

发现错误。结案。使用此代码拆分 cookie 字符串。

String temp = headerValue.substring(0, (len1-17));
于 2010-07-29T09:42:06.183 回答