httpurlconnection

Java HttpURLConnection.getInputStream but get 401 IOException

允我心安 提交于 2019-11-29 13:53:47
I am writing a REST client for CouchDB in Java. The following code should be quite standard: this.httpCnt.connect(); Map<String, String> responseHeaders = new HashMap<>(); int i = 1; while (true){ String headerKey = this.httpCnt.getHeaderFieldKey(i); if (headerKey == null) break; responseHeaders.put(headerKey, this.httpCnt.getHeaderField(i)); i++; } InputStreamReader reader = new InputStreamReader(this.httpCnt.getInputStream()); StringBuilder responseBuilder = new StringBuilder(); char[] buffer = new char[1024]; while(true){ int noCharRead = reader.read(buffer); if (noCharRead == -1){ reader

How to show progress bar status by percentage while uploading json data?

与世无争的帅哥 提交于 2019-11-29 13:27:55
I am uploading string and photo.and its working fine. Now I want to show progress bar while uploading data with percentage but percentage show very quickly to 100 percentage and take some more time to upload and finally come to the post execute method. protected class upload_images extends AsyncTask<String, Integer, String> { ProgressDialog progressDialog; @Override protected void onPreExecute() { super.onPreExecute(); // showDialog(progress_bar_type); progressDialog = new ProgressDialog(Accept_Report.this); progressDialog.setCancelable(false); // dialog.setCanceledOnTouchOutside(false);

In Java, how close the connection and free the port/socket using HttpURLConnection?

巧了我就是萌 提交于 2019-11-29 12:08:45
I'm using HttpURLConnection to do download of pages in Java. I forgot to release the connections and I think this was causing some problems to my system (a webcrawler). Now I've done some tests and see that after disconnecting, some connections still like TIME_WAIT in the results from the netstat command on Windows. How I do to free this connection immediately? Example code: private HttpURLConnection connection; boolean openConnection(String url) { try { URL urlDownload = new URL(url); connection = (HttpURLConnection) urlDownload.openConnection(); connection.setInstanceFollowRedirects(true);

Where does the socket timeout of 21000 ms come from?

廉价感情. 提交于 2019-11-29 12:05:57
The Problem An app I'm maintaining keeps getting socket timeouts after approximately 21000 ms, despite the fact that I've explicitly set longer timeouts. This seemingly magical value of 21000 ms has come up in a few other SO questions and answers, and I'm trying to figure out exactly where it comes from. Here's the essence of my code: HttpURLConnection connection = null; try { URL url = new URL(urlString); connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(45000); connection.setReadTimeout(90000); int responseCode = connection.getResponseCode(); if

HttpURLConnection GET request getting 400 Bad Request

限于喜欢 提交于 2019-11-29 11:44:53
I am trying to do a GET request with some parameters in Java using HttpURLConnection. Everytime I do this however, I get a 400: Bad Request each time. What do I need to change to make it work? String url = "http://www.awebsite.com/apath?p1=v1&p2=v2&p3=v3"; HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection(); conn.setDoInput(true); conn.setDoOutput(false); conn.setUseCaches(false); conn.setRequestMethod("GET"); conn.setRequestProperty("Host", "www.awebsite.com"); conn.setRequestProperty("User-Agent", "Mozilla/4.0"); conn.setRequestProperty("Accept", "text/html,application

Why would URLConnection timeout after 6+ minutes instead of 5 seconds?

白昼怎懂夜的黑 提交于 2019-11-29 10:23:03
I am copying this method verbatim from my application, which isn't complete yet but it does attempt to provide me with a timeout stack trace if things don't go smoothly: protected boolean isHttpAlive() { boolean isHttpOk = false; HttpURLConnection httpConnection = null; try { URL gurl = new URL("http://www.amazon.com/"); URLConnection connection = gurl.openConnection(); connection.setConnectTimeout(5 * 1000); // 5 seconds! httpConnection = (HttpURLConnection) connection; int responseCode = httpConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) isHttpOk = true; } catch

Java HTTPUrlConnection timeout does not work

百般思念 提交于 2019-11-29 09:41:24
I've written a programm that opens a httpurlconnection to a website through random proxies. My httpurlconnection is called conn . Now I know, that some of those proxies might be too slow, so i've set the timeout of the connection to 40000 milliseconds with conn.setConnectTimeout(40000) and conn.setReadTimeout(40000) . After doing so, i got this code: long diff = 0; long starttime = 0; long endtime = 0; try { starttime = System.currentTimeMillis(); conn.connect(); endtime = System.currentTimeMillis(); diff = endtime - starttime; if (endtime <= starttime + conn.getConnectTimeout()) { //Trying to

Sending json object via http post method in android

﹥>﹥吖頭↗ 提交于 2019-11-29 08:57:27
its NOT DUPLICATE.Link that has been provided is an OLD one."http client" has been removed in api23 I want to send json object: {"emailId":"ashish.bhatt@mobimedia.in","address":"Naya bans","city":"Noida","pincode":"201301","account_number":"91123546374208","bank_name":"Axis Bank","branch_name":"91123546374208","ifsc_code":"UTI0000879"} to url: http://10digimr.mobimedia.in/api/mobile_retailer/update_profile How do i do it? via post method? METHOD: POST /api/mobile_retailer/update_profile MANDATORY KEY: {"emailId","address"} REQUEST JSON: {"emailId":"ashish.bhatt@mobimedia.in","address":"Naya

Making PUT request with JSON data using HttpURLConnection is not working

走远了吗. 提交于 2019-11-29 08:24:28
问题 I'm trying to make PUT request with JSON data using HttpURLConnection in Java. The way I do it doesn't work. I get no errors so I don't know what the problem is. public static void main(String[] args) { URL url; try { url = new URL("http://fltspc.itu.dk/widget/515318fe17450f312b00153d/"); HttpURLConnection hurl = (HttpURLConnection) url.openConnection(); hurl.setRequestMethod("PUT"); hurl.setDoOutput(true); hurl.setRequestProperty("Content-Type", "application/json"); hurl.setRequestProperty(

Using browser's certificate in java program

你离开我真会死。 提交于 2019-11-29 07:27:28
I am trying to make HTTP GET request using HttpURLConnection in java. When I make get using browser it says me certificate is not trusted do you want to proceed. I accept certificate and GET request get data. but i am getting certificate exception in java( given below ) What i understood from this exception is, I need to download that certificate and put this java system property berfore making GET request. My questions are. How will download this certificate from browser? Can I use browser's certificate store in my java program, what do I need to know to use that? If i want to install