1

Hi i am trying do a server request using HttpURLConnection POST method , but not receiving any response, i saw few posts here in SO but i am unable understand them . Below is my code

@Override
        protected String doInBackground(Void... arg0) {
            try{
                 System.out.println("inside try clockr=====");
            String urlParameters = "Username=sadmin&Password=s4dm1ncoe";
            String request = "http://10.xx.xx.xxx/eai_enu/start.swe?SWEExtSource=WebService&SWEExtCmd=Execute&Username=sadmin&Password=s4dm1ncoe";
            URL url = new URL(request); 
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();           
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setInstanceFollowRedirects(false); 
            connection.setRequestMethod("POST"); 
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
            connection.setRequestProperty("charset", "utf-8");
            connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
            connection.setUseCaches (false);

            DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();
            connection.connect();
            System.out.println("checking response=====");
            //Get Response  
            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer(); 
            while((line = rd.readLine()) != null) {
              response.append(line);
              response.append('\r');
            }
            rd.close();
            System.out.println("checking response====="+response.toString());
            return response.toString();


            //connection.disconnect();

        }catch(Exception e){
                 System.out.println("checking error====="+e.toString());
                 return new String("Exception: " + e.getMessage());
             }
        }   
    }

The above code gives me error FilenotfoundExcepttion stacktrace as below

07-15 12:35:47.656: I/System.out(30352): checking error=====java.io.FileNotFoundException: http://10.xx.xx.xxx/eai_enu/start.swe?SWEExtSource=WebService&SWEExtCmd=Execute&Username=sadmin&Password=s4dm1ncoe

This is my URL http://10.xx.xx.xxx/eai_enu/start.swe?SWEExtSource=WebService&SWEExtCmd=Execute&Username=sadmin&Password=s4dm1ncoe"

can someone help explain how to set parameters for my url?

teekib
  • 2,821
  • 10
  • 39
  • 75

1 Answers1

0

You should set both setConnectTimeout() & setReadTimeout() and do your timeout work in catch() exception block.

try {

    String path = "http://www.your.web.path/";

    HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
    conn.setConnectTimeout(5000);
    conn.setReadTimeout(5000);

    if(conn.getResponseCode() == 200){
      //...
    }

} catch (Exception e) {
    e.printStackTrace();
    //do your timeout work here.
} 

if setReadTimeout() is ignored, some problems may occured, see Here.

Community
  • 1
  • 1
Eddy
  • 3,623
  • 37
  • 44