urlconnection

How can I specify the local address on a java.net.URLConnection?

南笙酒味 提交于 2019-11-26 13:20:49
问题 My Tomcat instance is listening to multiple IP addresses, but I want to control which source IP address is used when opening a URLConnection . How can I specify this? 回答1: This should do the trick: URL url = new URL(yourUrlHere); Proxy proxy = new Proxy(Proxy.Type.DIRECT, new InetSocketAddress( InetAddress.getByAddress( new byte[]{your, ip, interface, here}), yourTcpPortHere)); URLConnection conn = url.openConnection(proxy); And you are done. Dont forget to handle exceptions nicely and off

Java URLConnection Timeout

China☆狼群 提交于 2019-11-26 12:10:04
问题 I am trying to parse an XML file from an HTTP URL. I want to configure a timeout of 15 seconds if the XML fetch takes longer than that, I want to report a timeout. For some reason, the setConnectTimeout and setReadTimeout do not work. Here\'s the code: URL url = new URL(\"http://www.myurl.com/sample.xml\"); URLConnection urlConn = url.openConnection(); urlConn.setConnectTimeout(15000); urlConn.setReadTimeout(15000); urlConn.setAllowUserInteraction(false); urlConn.setDoOutput(true);

Why would a “java.net.ConnectException: Connection timed out” exception occur when URL is up?

泄露秘密 提交于 2019-11-26 10:19:44
问题 I\'m getting a ConnectException: Connection timed out with some frequency from my code. The URL I am trying to hit is up. The same code works for some users, but not others. It seems like once one user starts to get this exception they continue to get the exception. Here is the stack trace: java.net.ConnectException: Connection timed out Caused by: java.net.ConnectException: Connection timed out at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect

Upload files from Java client to a HTTP server

霸气de小男生 提交于 2019-11-26 10:13:56
I'd like to upload a few files to a HTTP server. Basically what I need is some sort of a POST request to the server with a few parameters and the files. I've seen examples of just uploading files, but didn't find how to also pass additional parameters. What's the simplest and free solution of doing this? Does anyone have any file upload examples that I could study? I've been googling for a few hours, but (maybe it's just one of those days) couldn't find exactly what I needed. The best solution would be something that doesn't involve any third party classes or libraries. BalusC You'd normally

URLConnection or HTTPClient : Which offers better functionality and more efficiency?

孤街浪徒 提交于 2019-11-26 09:02:46
问题 I looking to create a login form for an android application. I want to use a post method to send information to the server side where it is handle by a PHP file; which in turn validates the parameters and sends back a response. I\'ve look through implementations using HttpClient and URLConnection, they are very similar.Which is more efficient for utilization within an android app? Thanks Fabii 回答1: I believe in this case it's up to whichever API you find more natural. Generally, HTTPClient is

Java: resume Download in URLConnection

前提是你 提交于 2019-11-26 08:22:27
问题 I wrote a program that downloads some files from some servers. Currently program works properly. But I want to add resume support to it. I\'m doing it like this But the result file is corrupted: .... File fcheck=new File(SaveDir+\"/\"+filename); if(resumebox.isSelected() && fcheck.exists()){ connection.setRequestProperty(\"Range\", \"Bytes=\"+(fcheck.length())+\"-\"); } connection.setDoInput(true); connection.setDoOutput(true); BufferedInputStream in = new BufferedInputStream (connection

What exactly does URLConnection.setDoOutput() affect?

限于喜欢 提交于 2019-11-26 07:29:44
问题 There\'s setDoOutput() in URLConnection . According to documentation I should Set the DoOutput flag to true if you intend to use the URL connection for output, false if not. Now I\'m facing exactly this problem - the Java runtime converts the request to POST once setDoOutput(true) is called and the server only responds to GET requests. I want to understand what happens if I remove that setDoOutput(true) from the code. What exactly will this affect? Suppose I set it to false - what can I do

Java: how to use UrlConnection to post request with authorization?

大兔子大兔子 提交于 2019-11-26 06:26:17
问题 I would like to generate POST request to a server which requires authentication. I tried to use the following method: private synchronized String CreateNewProductPOST (String urlString, String encodedString, String title, String content, Double price, String tags) { String data = \"product[title]=\" + URLEncoder.encode(title) + \"&product[content]=\" + URLEncoder.encode(content) + \"&product[price]=\" + URLEncoder.encode(price.toString()) + \"&tags=\" + tags; try { URL url = new URL(urlString

Can you explain the HttpURLConnection connection process?

夙愿已清 提交于 2019-11-26 06:09:38
问题 I am using HTTPURLConnection to connect to a web service. I know how to use HTTPURLConnection but I want to understand how it works. Basically, I want to know the following: On which point does HTTPURLConnection try to establish a connection to the given URL? On which point can I know that I was able to successfully establish a connection? Are establishing a connection and sending the actual request done in one step/method call? What method is it? Can you explain the function of

Upload files from Java client to a HTTP server

一个人想着一个人 提交于 2019-11-26 02:05:57
问题 I\'d like to upload a few files to a HTTP server. Basically what I need is some sort of a POST request to the server with a few parameters and the files. I\'ve seen examples of just uploading files, but didn\'t find how to also pass additional parameters. What\'s the simplest and free solution of doing this? Does anyone have any file upload examples that I could study? I\'ve been googling for a few hours, but (maybe it\'s just one of those days) couldn\'t find exactly what I needed. The best