简体   繁体   中英

How to make HttpURLConnection get header from servlet response?

Servlet code:

if (errorMessage!=null) {
   response.setHeader("errorMessage",errorMessage);
   response.setStatus(200);
 } else {
   response.setStatus(500);
 }

Client Code:

HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
if (httpCon.getResponseCode==200 && httpCon.getHeaderField("errorMessage")!=null) {
   out.println("Error: "+ httpCon.getHeaderField("errorMessage"));
 } else {
     out.println("Error 500");
 }

The problem is that

httpCon.getHeaderField("errorMessage")

is always coming as null though errorMessage is not always null.

What am I doing wrong?

You should be using getResponseMessage() .

But 200 means no error. It means OK. Why are you expecting an error message?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM