#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    New Jersey
    Posts
    100
    Rep Power
    12

    Lightbulb Java - HTTPStatus Code


    When receiving an XML transmission, how do I retrieve the HTTPStatus Code using Java?

    Thanks,

    Robin
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    How are you receiving this transmission, from a request to a web server? Are you talking about the status code sent as a response header, like 404 or 200? Can I see what code you have so far?
    Last edited by Nemi; April 21st, 2003 at 12:40 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    New Jersey
    Posts
    100
    Rep Power
    12
    Nemi,

    Thanks for responding. According to UPS, when receiving an XML response, there are 4 possible HTTP Status Codes:

    200 – Request processed successfully.
    240 – Request processed. Some warnings exist.
    250 – Request could not be processed.
    500 – UPS OnLine Tool unavailable; try again later.

    The Java code below is a combination of Sun’s sample code and my own. I would prefer not to send the response to the screen as they do but don’t know how to proceed in terms of putting the response into a String variable as is done with this ASP code:

    Set objSrvHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")
    objSrvHTTP.open "POST", "https://wwwcie.ups.com/ups.app/xml/Rate",false
    objSrvHTTP.SetRequestHeader "Content-Type","application/x-www-form-urlencoded"
    objSrvHTTP.send (strMessage)
    strResponseMessage = objSrvHTTP.responseText
    intHTTPStatusCode = objSrvHTTP.status
    *****************************************
    Java code:

    import java.io.*;
    import java.util.*;
    import java.net.URL;

    import javax.servlet.*;
    import javax.servlet.http.*;

    import javax.xml.soap.*;
    import javax.xml.transform.*;
    import javax.xml.transform.stream.*;


    public class TestUPSTransmit extends HttpServlet
    {

    static final String NEWLINE = "\n";
    static final String UPS_URL = "https://wwwcie.ups.com/ups.app/xml/Rate";

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {

    try
    {
    response.setContentType("text/xml");

    URL endpoint = new URL(UPS_URL);

    SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
    SOAPConnection connection = scf.createConnection();

    MessageFactory mf = MessageFactory.newInstance();

    // Create a message from the message factory.
    SOAPMessage msg = mf.createMessage();

    SOAPPart soapPart=msg.getSOAPPart();
    SOAPEnvelope envelope = soapPart.getEnvelope();

    String UPS_Header_File = "UPSTrans" + "\\" + "UPSHeader.xml";
    String UPS_Body_File = "UPSTrans" + "\\" + "UPSBody.xml";

    String UPS_Path = getServletContext().getRealPath("/");

    if (UPS_Path.indexOf("SSL") > -1)
    {
    int intSSLSection = UPS_Path.indexOf("SSL");
    String strRelPath = UPS_Path.substring(0, intSSLSection);
    UPS_Path = strRelPath;
    }

    String strFileHeader = UPS_Path + UPS_Header_File;
    String strFileBody = UPS_Path + UPS_Body_File;

    BufferedReader reader1 = new BufferedReader(new FileReader(strFileHeader));
    BufferedReader reader2 = new BufferedReader(new FileReader(strFileBody));
    StringBuffer buf = new StringBuffer();

    String strLine1;
    String strLine2;

    while ((strLine1 = reader1.readLine() ) != null)
    {
    buf.append(strLine1);
    buf.append(NEWLINE);
    }

    while ((strLine2 = reader2.readLine() ) != null)
    {
    buf.append(strLine2);
    buf.append(NEWLINE);
    }

    String strMessage = buf.toString();
    StringReader strReader = new StringReader(strMessage);
    StreamSource ssrc=new StreamSource(strReader);
    soapPart.setContent(ssrc);

    msg.saveChanges();

    SOAPMessage reply = connection.call(msg, endpoint);

    // Display
    // Document source, do a transform.
    System.out.println("Result:");
    TransformerFactory tFact=TransformerFactory.newInstance();
    Transformer transformer = tFact.newTransformer();
    Source src=reply.getSOAPPart().getContent();
    StreamResult result=new StreamResult( System.out );
    transformer.transform(src, result);
    System.out.println();

    connection.close();

    }
    catch(Throwable e)
    {
    e.printStackTrace();
    }
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
    doPost(request, response);
    }

    }

    *******************************************************
    Robin
  6. #4
  7. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    I have not used SOAP, so I am not very familiar with it. However, looking briefly it appears you may have to get the status code using the SOAPFault object. It appears to be located
    Code:
    soapMessage.getSOAPPart().getEnvelope().getBody().getFault().getFaultCode();
    Here is some light reading I found that may or may not be of some help.
    http://access1.sun.com/techarticles/...XM_Faults.html

    GL

IMN logo majestic logo threadwatch logo seochat tools logo