Tag: httpurlconnection

Android FileNotFoundexception – 无法从没有文件格式的图像URL获取getInputStream

标题非常自我解释。 以下代码……: URL imageUrl = new URL(url); try { HttpURLConnection conn= (HttpURLConnection)imageUrl.openConnection(); conn.setDoInput(true); conn.connect(); int length = conn.getContentLength(); InputStream is = conn.getInputStream(); return BitmapFactory.decodeStream(is); } catch (IOException e) { e.printStackTrace(); } 如果url不包含文件格式,则会失败。 例如,谷歌托管的一些图像将显示图像,但不具有文件格式(.png,.jpg等)作为url的一部分。 因此,url连接的content-type标头返回“text / html”。 我相信这是个问题。 我试过设置一个新的内容类型标题,但似乎没有改变任何东西。 有人知道解决方法吗? 谢谢。

Java URLConnection – 我什么时候需要使用connect()方法?

我有一个问题需要理解URLConnection类中connect()方法的含义。 在下面的代码中,如果我使用connect()方法,如果我不使用它,我会得到相同的结果。 为什么(或何时)需要使用它? URL u = new URL(“http://example.com”); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.connect();//with or without it I have the same result InputStream in = conn.getInputStream(); int b; while ((b = in.read()) != -1) { System.out.write(b); }

HttpURLConnection实现

我已经读过HttpURLConnection支持持久连接,因此可以为多个请求重用连接。 我尝试了它,发送第二个POST的唯一方法是第二次调用openConnection。 否则我得到一个IllegalStateException(“已经连接”); 我使用了以下内容: try{ URL url = new URL(“http://someconection.com”); } catch(Exception e){} HttpURLConnection con = (HttpURLConnection) url.openConnection(); //set output, input etc //send POST //Receive response //Read whole response //close input stream con.disconnect();//have also tested commenting this out con = (HttpURLConnection) url.openConnection(); //Send new POST 第二个请求是通过相同的TCP连接发送的(通过wiresharkvalidation)但我无法理解为什么(虽然这是我想要的)因为我已经调用了disconnect。 我检查了HttpURLConnection的源代码,并且实现确实保持了对相同目标的连接的keepalive缓存。 我的问题是,在发送第一个请求后,我无法看到连接如何放回缓存中。 断开连接关闭连接,没有断开连接,仍然无法看到连接如何放回缓存。 我看到缓存有一个run方法来遍历所有空闲连接(我不确定它是如何被调用的),但是我找不到连接如何放回缓存中。 似乎唯一发生的地方是httpClient的完成方法,但是没有调用具有响应的POST。 谁可以帮我这个事? 编辑我的兴趣是,对于tcp连接重用,HttpUrlConnection对象的正确处理是什么。 应该关闭输入/输出流,然后是url.openConnection(); 每次发送新请求(避免disconnect())? […]

我可以覆盖使用java的HttpUrlConnection类的Host头吗?

我正在使用以下代码在java中打开http连接: URL url = new URL(“http://stackoverflow.com”); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod(“GET”); conn.setRequestProperty(“Host”, “Test:8080”); conn.getOutputStream(); 但是,调用conn.setRequestProperty(“Host”,“Test:8080”)似乎无效,无论我调用方法的顺序如何,主机都重置为目标服务器。 有没有办法在不使用其他库的情况下覆盖Host头? TIA Matt

如何使用HttpURLConnection获取重定向的URL和内容

有时我的URL会重定向到新页面,所以我想获取新页面的URL。 这是我的代码: URL url = new URL(“http://stackoverflow.com/questions/88326/”); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setInstanceFollowRedirects(true); System.out.println(conn.getURL().toString()); 输出是: stackoverflow.com/questions/88326/does-elmah-handle-caught-exceptions-as-well 它适用于Stack Overflow网站,但对于sears.com网站,它不起作用。 如果我们输入URL打击: http://www.sears.com/search=iphone 输出仍然是: http://www.sears.com/search=iphone 但实际上,该页面将重定向到: http://www.sears.com/tvs-electronics-phones-all-cell-phones/s-1231477012?keyword=iphone&autoRedirect=true&viewItems=25&redirectType=CAT_REC_PRED 我怎么解决这个问题?

如何重用HttpUrlConnection?

我有兴趣重用一个HttpUrlConnection(作为我正在开发的服务器和客户端之间的状态协议的一部分)。 我知道持久性http有一个Connection = keep-alive标头。 现在,我想知道如何重用这样一个问题。 我写了这段代码: URL u = new java.net.URL(“http://localhost:8080/Abc/Def”); HttpURLConnection c = (HttpURLConnection) u.openConnection(); c.setRequestMethod(“GET”); c.setRequestProperty(“Connection”, “keep-alive”); c.setHeader(“A”,”B”); c.getInputStream() //here I see that server gets my messages (using DEBUG) c.setHeader(“B”,”C”); // 现在我如何重新发送这个“B”标头到服务器,我尝试重新连接等,但没有任何东西让它工作。 并且服务器还执行response.setHeader(“Connection”, “keep-alive”); 我看过许多论坛,但没有人写过这个。 也许HttpURLConnection不处理这个?

读取输入后无法写入输出

由于HttpURLConnection ,我正在编写一个连接到servlet的程序,但是在检查url时我卡住了 public void connect (String method) throws Exception { server = (HttpURLConnection) url.openConnection (); server.setDoInput (true); server.setDoOutput (true); server.setUseCaches (false); server.setRequestMethod (method); server.setRequestProperty (“Content-Type”, “application / xml”); server.connect (); /*if (server.getResponseCode () == 200) { System.out.println (“Connection OK at the url:” + url); System.out.println (“——————————————- ——- “); } else System.out.println (“Connection failed”); }*/ 我收到了错误: […]

Java简单代码:java.net.SocketException:来自服务器的文件意外结束

我在Java中编写了一些简单的代码,该方法应该连接到网站并返回BufferedReader。 private BufferedReader getConnection(String url_a) { URL url; try { System.out.println(“getting connection”); url = new URL(url_a); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.addRequestProperty(“User-Agent”, “Mozilla/5.0 (X11; U; Linux i586; en-US; rv:1.7.3) Gecko/20040924” + “Epiphany/1.4.4 (Ubuntu)”); inStream = new InputStreamReader(urlConnection.getInputStream()); return new BufferedReader(inStream); } catch (Exception ex) { Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex); } return null; } 当我在我的电脑上使用它时,它工作正常,但当我把.jar文件放在服务器上时,我收到此错误: java.net.SocketException: Unexpected […]

cURL和HttpURLConnection – 发布JSON数据

如何使用HttpURLConnection发布JSON数据? 我正在尝试这个: HttpURLConnection httpcon = (HttpURLConnection) ((new URL(“a url”).openConnection())); httpcon.setDoOutput(true); httpcon.setRequestProperty(“Content-Type”, “application/json”); httpcon.setRequestProperty(“Accept”, “application/json”); httpcon.setRequestMethod(“POST”); httpcon.connect(); StringReader reader = new StringReader(“{‘value’: 7.5}”); OutputStream os = httpcon.getOutputStream(); char[] buffer = new char[4096]; int bytes_read; while((bytes_read = reader.read(buffer)) != -1) { os.write(buffer, 0, bytes_read);// I am getting compilation error here } os.close(); 我在第14行遇到编译错误。 cURL请求是: curl -H “Accept: […]

是否可以仅使用JDK或HttpComponents处理JSON响应?

我们正在升级我们的网络应用程序以使用Facebook的Graph API,它返回JSON响应。 但是,我们不希望将依赖性添加到JSON库,除非我们没有其他选择。 对于服务器端的http请求,我们使用Apache HttpComponents。 因此,我的问题是我可以用来处理JSON响应的JDK和/或HttpComponents中的类(如果有的话)是什么? 欢迎使用代码片段:)