我正在使用下面的代码,它为一些用户发出多个POST请求,即使它只针对一个请求进行编码,对我来说它工作正常并且getresponseCode返回200,而对于其他一些用户则在getresponseCode上给出空指针异常。
URL url = new URL(strURL);
lobjConn = (java.net.HttpURLConnection) url.openConnection();`enter code here`
lobjConn.setRequestProperty("Cookie", "mysession="+smCookie);
lobjConn.setDoInput(true);
lobjConn.setDoOutput(true);
lobjConn.setUseCaches(false);
lobjConn.setRequestMethod("POST");
lobjConn.setRequestProperty("Connection", "Keep-Alive");
lobjConn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + BOUNDARY);
lobjOS = new DataOutputStream(lobjConn.getOutputStream());
int rc = lobjConn.getResponseCode();
当执行到达上面的最后一行时,它会抛出NPE,当我们检查登录服务器时,它显示2个请求到达服务器,即使我们没有处于任何循环中。
例外:
java.lang.NullPointerException at
sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:719) at
sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:646) at
COM.ibm.JEmpower.applet.http.HttpURLConnection.getInputStream(HttpURLConnection.java:411) at
COM.ibm.JEmpower.applet.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:703) at
java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:399) at
WBDocSubmitOffline.getObjectId(WBDocSubmitOffline.java:240) at
WBDocSubmitOffline.NotesMain(WBDocSubmitOffline.java:98) at
lotus.domino.AgentBase.runNotes(Unknown Source)
答案 0 :(得分:0)
在发送任何内容之前获取响应代码毫无意义。您应该首先发送请求,然后获取响应代码,然后在需要时获取响应。写的代码没有意义。