0

我正在使用下面的代码,即使它仅针对一个请求进行编码,它也会为某些用户发出多个 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)
4

1 回答 1

0

在您发送任何内容之前获取响应代码是没有意义的。您应该先发送请求,然后获取响应代码,然后在需要时获取响应。编写的代码没有意义。

于 2013-11-13T05:16:58.330 回答