httpurlconnection

All trusting HostnameVerifier causes SSL errors with HttpURLConnection

孤街浪徒 提交于 2019-11-29 07:26:13
I'm using HttpURLConnection to connect to SSL sites. Occasionally these use self signed certificates / otherwise badly behaved SSL so I have a mode where these an be accessed. I'm using the typical code recommended many places online: // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[]{ new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {} public void checkServerTrusted(java.security

Getting a reference to Java's default http(s) URLStreamHandler

柔情痞子 提交于 2019-11-29 04:33:29
I have a library that I need to use in one of my projects, which unfortunately registers its own URLStreamHandler to handle http- URLs . Is there a way to get a reference to Java's default http- and https- URLStreamHandlers , so I can specify one of them in the URL 's constructor to open a standard http connection without using the protocol overridden by the library? Found it: sun.net.www.protocol.http.Handler With that, I can now do: URL url = new URL(null, "http://...", new sun.net.www.protocol.http.Handler()); HttpURLConnection cxn = (HttpURLConnection) url.openConnection(); And I get a

How to upload binary file using URLConnection

半腔热情 提交于 2019-11-29 03:56:10
In order to upload a binary file to an URL, I have been advised to use this guide . However, the file is not in a directory, but is stored in a BLOB field in MySql db. The BLOB field is mapped as a byte[] property in JPA: byte[] binaryFile; I have slightly modified the code taken from the guide, in this way: HttpURLConnection connection = (HttpURLConnection ) new URL(url).openConnection(); // set some connection properties OutputStream output = connection.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, CHARSET), true); // set some headers with writer

Why am I getting this error Premature end of file?

点点圈 提交于 2019-11-29 02:56:41
I am trying to parse and XML response , but I am failing miserably. I thought initially that the xml was just not being returned in the response, so I crafted up the code below with a direct link to my xml file online. I am able to print the XML to screen with no problems. However when I call my parse method I get Premature end of file. It works if I pass the URL directly: builder.parse(""); but fails when I passed an InputStream: builder.parse(connection.getInputStream()); try { URL url = new URL(xml); URLConnection uc = url.openConnection(); HttpURLConnection connection = (HttpURLConnection

How to implement Login with HttpURLConnection and PHP server in Android

心不动则不痛 提交于 2019-11-29 02:46:57
I have a Web Service which has login functionality, connecting to a PHP back end: Htp://hfbhgvbbnvhn.com/mypictures/loginService.php (its in fact http) I have to send two parameters to it and in return I would get the JSONObject. But right now I am trying to use the HttpUrlConnection object to connect to the PHP service, but I have no idea to how to send the parameters. I have searched many solutions but they all are working on NameValuePair with DefaultHttpClient where as I think I have to send two separate strings as a parameters. So How can I convert this code to an implementation that uses

How to send an image from Android client to Node.js server via HttpUrlConnection?

元气小坏坏 提交于 2019-11-29 02:44:54
I am trying to send an image to the server using HttpUrlConnection, because it's recommended by Google. I decided to convert the image into Base64 string and send it to the server where I decoded it into .jpg file. But this method is only feasible with small-sized thumbnails and I cannot send a full-sized images. Here is the android client code: public static void postData(Bitmap imageToSend) { try { URL url = new URL("http://"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); conn

Android: Trying to get data from youtube api

穿精又带淫゛_ 提交于 2019-11-29 02:40:13
I'm attempting to get data from http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured?&start-index=1&max-results=15&v=2 and print the titles of the videos, but I get an exception in the line int responseCode = httpConnection.getResponseCode(); what's the problem? Any other way to get this data? My code: URL url; try { String featuredFeed = "http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured?&start-index=1&max-results=15&v=2"; url = new URL(featuredFeed); URLConnection connection; connection = url.openConnection(); HttpURLConnection httpConnection =

HttpURLConnection downloaded file name

元气小坏坏 提交于 2019-11-29 02:32:37
问题 Is it possible to get the name of a file downloaded with HttpURLConnection? URL url = new URL("http://somesite/getFile?id=12345"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setAllowUserInteraction(false); conn.setDoInput(true); conn.setDoOutput(true); conn.connect(); InputStream is = conn.getInputStream(); In the example above I cannot extract the file name from the URL, but the server will send me the file name in some way. 回答1: You

Android: How to programatically login to website and retrieve data from it?

旧街凉风 提交于 2019-11-29 01:58:34
问题 Ok, guys, so I'm recently developing android app that saves user's ID and PASSWORD to SharedPreferences. Now, when the user starts app second time, he will be redirected directly to MainActivity with a few options in listView. And now I have a huge headache and it's driving me REAL crazy. I cannot login to website and fetch the data to my phone. I've tried using Http(s)UrlConnection, HttpClient, but it just doesn't seem to work for me. All I get from POST method is a source code of login page

Android HttpURLConnection throwing EOFException

这一生的挚爱 提交于 2019-11-29 01:28:25
Here's the question in simplest way. I create a HTTPS connection to my server through proxy using HttpUrlConnection Object. My proxy closes the connection but my code still tries to reuse the same connection. And so I get EOFException. How do I handle such cases? kturney I'd recommend disabling the http.keepalive system property. The performance section of the documentation indicates that socket connections will be reused when possible. It sounds like it is trying to reuse the connection, but the proxy has already closed it. On this post , Darrell also indicates that changing the system