在servlet中使用POST方法时接收null值?

时间:2016-08-03 10:02:54

标签: java servlets

我想在servlet中使用POST方法发送一个字符串,以便它可以与其他域中存在的其他servlet应用程序通信。但我在输入蒸汽对象中收到null。

我的代码:

    String content = "10 141 nahush123 01";
    URL url = new URL("http://52.220.37.12:8080/servers/servers");
    System.out.println(url.toString());
    HttpURLConnection httpCon = (HttpURLConnection)url.openConnection();
    httpCon.setDoInput(true);
    httpCon.setDoOutput(true);
    httpCon.setRequestMethod("POST");
    httpCon.setRequestProperty("Content-Type", "text/plain;charset=UTF-8");

    OutputStream os = httpCon.getOutputStream();
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(os));
    out.write(content);
    //br = BufferReader(out);
    System.out.println(httpCon.getResponseCode());
    System.out.println(httpCon.getResponseMessage());
    writer.println(httpCon.getResponseMessage());


    httpCon.connect();
    br = new BufferedReader(new InputStreamReader(httpCon.getInputStream()));
    //reading response 
    //String resp;
    //if(br != null){
        content = br.readLine();
    //}
    System.out.println(content);
    writer.println(content);
    out.flush();
    writer.flush();
    writer.close();
    out.close();
    os.close();

3 个答案:

答案 0 :(得分:1)

复制粘贴你的servlet中的代码工作正常

  1. 检查您是否覆盖了servlet中的doPost方法

  2. 检查您的帖子metod工作正常并返回值,我使用"DHC Rest Client"插件进行chrome。这是非常酷的工具

  3. 尝试删除httpCon.connect()行。没有必要。

    关于connect()方法的oracle文档:

  4.   

    依赖于连接的操作,如getContentLength,将会   如有必要,隐式执行连接。

    操作getResponseMessage,getResponseCode执行连接。

    已更新

    我尝试使用我的本地测试网址进行测试,但它确实有效。然后我尝试使用你的网址,它不起作用。

    您需要在使用输入流之前刷新输出流。

    此代码工作并返回" 0#5#26#温度#Living_Room#555555581#0 + 0#7#0#Room1#Master_bedroom#555555581#1 + 1#7#0#Door1 #Living_Room# 555555581#2 + 1#6#0#frontdoor#Living_Room#555555581#2 + 0#6#0#doorback#阳台#555555581#2 +"

    PrintWriter writer = res.getWriter();
    String content = "10 141 nahush123 01";
    URL url = new URL("http://52.220.37.12:8080/servers/servers");
    System.out.println(url.toString());
    HttpURLConnection httpCon = (HttpURLConnection)url.openConnection();
    httpCon.setDoInput(true);
    httpCon.setDoOutput(true);
    httpCon.setRequestMethod("POST");
    httpCon.setRequestProperty("Content-Type", "text/plain;charset=UTF-8");
    
    OutputStream os = httpCon.getOutputStream();
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(os));
    out.write(content);
    out.flush();
    //br = BufferReader(out);
    System.out.println(httpCon.getResponseCode());
    System.out.println(httpCon.getResponseMessage());
    writer.println(httpCon.getResponseMessage());
    
    
    //httpCon.connect();
    BufferedReader br = new BufferedReader(new InputStreamReader(httpCon.getInputStream()));
    //reading response
    //String resp;
    //if(br != null){
    content = br.readLine();
    //}
    System.out.println(content);
    writer.println(content);
    writer.flush();
    writer.close();
    out.close();
    os.close();
    

答案 1 :(得分:0)

尝试使用servlet中的getparameter()函数

答案 2 :(得分:0)

使用传递字符串参数调用此方法:

public String postData(String reqStr) {
ObjectOutputStream objOS = null;
ObjectInputStream objIS = null;
HttpURLConnection servletCon = null;
try {
if (reqStr != null) {
Thread.sleep(1000);
objOS.writeObject(reqStr);
} else {
throw new Exception("request is null.");
}
} catch (Exception ex) {
        logger.error("Failed to send request to server.", ex);
    }
    String response = null;
    try {
        objIS = new ObjectInputStream(servletCon.getInputStream());
        response = (String) objIS.readObject();
    } catch (Exception ex) {
        logger.error("Failed to get response from server.", ex);
        response = null;
    }
    return response;
}

像你一样初始化连接,但呼叫方式不同:

public void initializeUrlConnection(String servletURL, String contentType, String method) {

    try {

        logger.debug("Servlet URL " + servletURL);
        url = new URL(servletURL);
        servletCon = (HttpURLConnection) url.openConnection();
        servletCon.setUseCaches(false);
        servletCon.setDefaultUseCaches(false);
        servletCon.setDoInput(true);
        servletCon.setDoOutput(true);
        servletCon.setRequestProperty("Content-Type", contentType);
        servletCon.setRequestMethod(method);
        objOS = new ObjectOutputStream(servletCon.getOutputStream());
    } catch (IOException ex) {

        logger.error("Failed to initialize Http Connection.", ex);
    }
}

首先使用必需参数初始化连接方法:

Object.initializeUrlConnection(url, contentType,method);

然后用你的字符串/ json参数调用posDataMethod:

Object.postData(str);