在没有数据交换的要求下启动服务器的servlet

在没有数据交换的要求下启动服务器的servlet

注:该程序目的是启动远程服务器上的servlet,但并不需要向服务器传送数据也不需要从服务器获得数据。

public class ImgThread implements Runnable {
    private static final int TIME_OUT = 60*1000;   //超时时间
    public static final String PRINTURL = "http://XXX/Servlet";

    public void run(){

        try {
            URL url = new URL(PRINTURL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(TIME_OUT);
            conn.setReadTimeout(TIME_OUT);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");

	/////////////////////////   (1)    ////////////////////////////		
            int res = conn.getResponseCode();
            Log.e("响应状态吗", "responseCode"+res);
            if(res==200){
                Log.e("servlet", "run");
            }
	//////////////////////////////////////////////////////////////

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

  如程序所示,如果没有程序段(1),则该程序无法启动服务器上Servlet中的doPost方法,无法启动服务器上的指定功能。故加上程序段(1),让服务器返回状态码,从而启动Servlet中的doPost方法,实现特定功能。一旦返回状态码为200,则说明一切正常,服务器已做响应。

附  Http响应状态码

原文地址:https://www.cnblogs.com/silentteen/p/6555515.html