httpurlconnection

Does java.net.HttpUrlConnection always throw IOException if there is an error status return by Server?

假如想象 提交于 2019-12-02 09:55:32
I am using java.net.HttpUrlConnection to make Http request to my Server. I realized that if the Server return an error status (400 for example). HttpUrlConnection will throw an IOException corresponding to the error status. My question is: Does HttpUrlConnection always throw an IOException if the server return an error status (4xx, 5xx)? I take a look at HttpUrlConnection API description but I couldn't answer my question. Updated: Please see my test: public static void main(String[] args) { try { String url = "https://www.googleapis.com/oauth2/v3/userinfo?access_token=___fake_token";

“no route to host” on device on getOutputStream() - connected wirelessly

♀尐吖头ヾ 提交于 2019-12-02 08:34:55
private String urlPost = "http://192.168.1.66:8080/DataCollectionServlet/"; @Override protected void doWakefulWork(Intent intent) { // https://stackoverflow.com/q/14630255/281545 HttpURLConnection connection = null; try { connection = connection(); w("connection"); // allrigt final OutputStream connOutStream = connection.getOutputStream(); w("GEToUTPUTsTREAM"); // I never see this } catch (IOException e) { e.printStackTrace(); // No route to host } finally { if (connection != null) connection.disconnect(); } } private HttpURLConnection connection() throws MalformedURLException, IOException {

HttpURLConnection, how to sending parameters via post?

試著忘記壹切 提交于 2019-12-02 08:26:24
String pathToOurFile = "/sdcard/DCIM/Camera/foto.jpg"; String urlServer = "http://server/upload.php"; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; . . . URL url = new URL(urlServer); connection = (HttpURLConnection) url.openConnection(); // Allow Inputs & Outputs connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); // Enable POST method connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

How to handle Arabic in java

北慕城南 提交于 2019-12-02 08:25:28
i need to pass a string of arabic value to the HttpURL as a parameter but before i do say when i print the message its showing only question marks public void sendSms(SendSms object) throws MalformedURLException,ProtocolException,IOException { String message=new String(object.getMessage().getBytes(), "UTF-8"); System.out.println(message);//printing only question marks } and even when i send the message as url parameter its not sending the original message as arabic its sending the question marks. public void sendSms(SendSms object) throws MalformedURLException, ProtocolException,IOException {

Make HttpURLConnection load web pages with images

寵の児 提交于 2019-12-02 07:49:25
问题 Currently I'm using HttpURLConnection for load remote web page and present to my clients (using InputStream to HttpResponse's outputStream transfer), it loads html correctly but skips images, how to fix it? Thanks 回答1: You need to manipulate the HTML that way so that all resource URLs on the intranet domain are proxied as well. E.g. all of the following resource references in HTML <base href="http://intranet.com/" /> <script src="http://intranet.com/script.js"></script> <link href="http:/

Receiving request timeout even though connect timeout and read timeout is set to default (infinite)?

痞子三分冷 提交于 2019-12-02 07:16:17
问题 I am connecting to a web service and did not set any connect timeout and read timeout on HttpURLConnection. What is the default connect timeout and read timeout? Is it dependent on the Android phone? Or does Android/ Java has its own default value for the timeout? When I try to get the value of my connect timeout ( getConnectTimeout() ) and read timeout ( getReadTimeout() ), it returns a value of 0 which I assume is the equivalent of infinity. However, even though their value is 0, there are

Receiving request timeout even though connect timeout and read timeout is set to default (infinite)?

对着背影说爱祢 提交于 2019-12-02 07:00:44
I am connecting to a web service and did not set any connect timeout and read timeout on HttpURLConnection. What is the default connect timeout and read timeout? Is it dependent on the Android phone? Or does Android/ Java has its own default value for the timeout? When I try to get the value of my connect timeout ( getConnectTimeout() ) and read timeout ( getReadTimeout() ), it returns a value of 0 which I assume is the equivalent of infinity. However, even though their value is 0, there are times that I'm still receiving a request timeout error. So their value can't be equal to infinity. Can

How to reading XML from a URL in Android

て烟熏妆下的殇ゞ 提交于 2019-12-02 05:19:26
I want to read a XML document from a URL : public void DownloadXmlFile() throws IOException{ //TODO String url = "http://api.m1858.com/coursebook.xml"; URL u = new URL(url); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.setReadTimeout(10000); conn.setConnectTimeout(15000); conn.setRequestMethod("GET"); conn.setDoInput(true); conn.connect(); } I get an Error Exception android.os.NetworkOnMainThreadException I added uses-permission in manifest file: <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS

sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to sun.net.www.protocol.http.HttpURLConnection

被刻印的时光 ゝ 提交于 2019-12-02 05:18:54
问题 i am using selenium library for testing purpose. But the following code giving me class cast exception. i have googled this exception but didn't get the solution. i am confused in Https connetion and http connetion. help me to solve this exception. thank you import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium

Cookie management with Java URLConnection

一世执手 提交于 2019-12-02 04:31:20
I'm fairly new to android programming and recently achieved a successful HTTP Post request, only to learn that my cookies are not being stored between subsequent Post/Get requests. I looked around the interweb, and found a few examples for Android's Apache client and Java's HttpURLConnection. I was unsuccessful in implementing either method into my current class, so I was wondering if someone with more experience could review my code and offer suggestions. Recap: My initial POST request is successful and authenticated. My second POST request does not retain the cookies from the initial POST