HttpURLConnection disconnect doesn't work in Android
The method disconnect from HttpURLConnection
seems not to work properly.
If I execute the following code:
url = new URL("http:// ...");
connection = (HttpURLConnection) url.openConnection ();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
// Some code
connection.disconnect();
connection.setDoInput(false); // -> IllegalStateException
I get an IllegalStateException
when I call the method setDoInput
. The exception says:
Already connected
Asked by: Melissa794 | Posted: 25-01-2022
Answer 1
It sounds like you're trying to reuse the connection? i.e. altering the request properties after you've disconnected from the server, ready to make another connection.
If that is the case, then just create a new HttpURLConnection
object.
Similar questions
Android HttpURLConnection disconnect
In my activity i am calling a url using httpurl connection.
In the case when the network strength is very low, i want to disconnect that request.
How can i disconnect an httpurl connection request that is already been sent?
I want to cancel the request when back button is pressed.
Android HttpURLConnection disconnect not closing
I am using HttpURLConnection to connect to server.And i want to cancel the connection by disconnecting the connection using conn.disconnect.But the connection is not getting cancelled.
Android: HttpURLConnection doesn't disconnect
by running netstat on the server:
UNTIL A FEW DAYS AGO: I could see the connection being ESTABLISHED only for about a second, and then it would disappear from the list
NOW: it stays as ESTABLISHED for about 10 seconds, then it goes into FIN_WAIT1 and FIN_WAIT2
the Android code is the same, the server is still ...
webserver - Using HttpURLConnection on Android
I am trying to connect a web server I have running in my android App however for some reason it's failing.
I can connect to the webpage normally and using my phone's browser, I can also use the below code to connect to google but for some reason my code will not connect to my web server. If I host the server on port 80 I get error code 503 every time, if I host it on 8080 (which is preferable) I get a timeout excep...
java - Optimizing HttpURLConnection in Android
this problem is bugging me:
HttpURLConnection con = (HttpURLConnection)new URL(url).openConnection();
con.setRequestMethod("HEAD");
if (con.getResponseCode()!=200 ){dosomething()}
Is this the correct way to set the Request Method, or is it already too late since I called URL.openConnection() and it already made the connection using the default which is GET?
I can't call setRequest...
Android can't send GET request with HttpURLConnection
I'm trying to use an HttpURLConnection in my application. I set my request method to 'GET', but when I try to retrieve the output stream then the method is changed to 'POST' !
I'm not sure that is the reason, but my JSON server (I use JAX-RS) returns a blank page when I send a request using a 'POST'.
Here is a snippet of my code:
// Create the connection
HttpURLConnection con = (HttpURLConnection) n...
InputStream returns 0 with HttpUrlConnection in Android
I'm doing a HttpUrlConnection in Android application with an URL. The URL is giving me response in the browser but in the code below InputStream response is 0. Why?
Here's my code:
URL url = new URL("https://certify.securenet.com/payment.scrnt");
HttpURLConnection httpCon = (HttpURLConnection)url.openConnection();
InputStream in = htt...
java - Authentication Problem..!! ON ANDROID WITH HTTPURLCONNECTION
"Basic YWRtaW46YW
RtaW4="
is right code for my address.I check on j2me project.
And on android my getbase64 method returns "Basic YWRtaW46YW
RtaW4=" its true.
and ı use it on:
httpConnection.setRequestProperty("Authorization",getBase64Encode());
Finally responce code is 401
any idea???
protected void connect() {
InputStream is = null;
OutputStream os = null;
...
android - The boundary string in HttpURLConnection?
Why we have to setup a boundary string?
What is the purpose for?
any rules on it?
Android 2.2 SDK / JAVA - error uploading file/posting params via HttpUrlConnection
I'm getting this error, though in my postMap I definitely specify a post var "eventTitle", what the heck is going on?:
01-03 11:52:29.758:
INFO/System.out(27682): Server
response is: Warning:
htmlspecialchars(): Invalid multibyte
sequence in argument in
/home/staging/public_html/system/library/request.php
on line 31Warning:
session_start(): Cannot send sessio...
HttpUrlConnection behaving different in Android 2.3 Gingerbread
I'm stumped here. I've been using the same HttpUrlConnection code to retrieve JSON from my server since Android 1.5. Suddenly, with 2.3 it won't work and I can't understand why.
Here's my code:
try {
HttpURLConnection conn =
(HttpURLConnection)(new URL(Constants.SERVER_URL + path)).openConnection();
conn.setAllowUserInteraction(false); // no user in...
java - Why android HttpURLConnection cache the inputstream results?
i'm trying to get a xml file but it seems to be cached.
there's my code:
URL url = new URL("http://delibere.asl3.liguria.it/SVILUPPO/elenco_xml.asp?rand=" + new Random().nextInt()+"&Oggetto=" + text +"&TipoDocumento="+tipoDocumento);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setDefaultUseCaches(false);
urlConn.setAllowUserInteraction(true);
urlConn.setDoInput(tru...
redirect - Android / Java: HttpURLConnection doesn't return headers of redirected file (e.g. on S3)
My code (reproduced below), connects to a url and downloads the file to disk on android. All standard stuff. When I try using this code on a file on S3 accessed via a subdomain on our server mapped to a bucket (e.g. foo.example.com => bucket called foo.example.com), it often fails. Turns out (using the handy curl command..
"curl -v -L -X GET http://foo.example.com/f/a.txt")
.. that th...
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android