URLConnection to WebService gets FileNotFoundException

Discussion in 'Java' started by Brock Jones, Sep 4, 2003.

  1. Brock Jones

    Brock Jones Guest

    I have a class that I use to generically connect and send data
    to/receive data from web services. The base functionality is provided
    by URLConnection.getInputStream() and URLConnection.getOutputStream().
    For virtually everything I've tried it on, it works like a charm.
    However, it seems to break using one particular service. When I try
    to send the data to that particular service, closing the stream
    results in a FileNotFoundException being thrown. The server logs an
    ambiguous error type 500 for the connection attempt. There is nothing
    special about this particular service -- it's virtually identical to
    several other services I use regularly.

    What follows is the gist of the code, dumbed down to the basics
    (originally, I was using a BufferedOutputStream to write the POST
    data).

    If someone could *please* post a response to this message or send an
    email to brockhjones (at) hotmail dot com, it would be appreciated
    very much!

    Thanks!
    -Brock

    public void processRequest( String strServiceUrl, String[] rgstrParams
    )
    {
    URLConnection urlc = null;
    String strUrl = strServiceUrl;
    String strResponse = null;

    try
    {
    try
    {
    // Create a URL object
    URL url = new URL( strUrl );
    // Open the URLConnection object
    urlc = url.openConnection();
    // Allow input
    urlc.setUseCaches( false );
    urlc.setDoInput( true );
    }
    catch( Exception e )
    {
    // failed to establish a connection
    return;
    }

    if( urlc == null )
    {
    // failed to establish a connection
    return;
    }

    if( ( m_rgstrParams != null ) && ( rgstrParams.length > 1 ) && ( (
    rgstrParams.length % 2 ) == 0 ) )
    {
    // We've got data to send. Set up the URLConnection
    // to allow output.
    urlc.setDoOutput( true );

    int iCount = rgstrParams.length / 2;
    String strParams = "";
    int idx = 0;
    for( int i = 0; i < iCount; i++ )
    {
    if( i > 0 )
    strParams += "&";

    strParams += rgstrParams[idx++] + "=" + rgstrParams[idx++];
    }


    try
    {
    OutputStream ostream = urlc.getOutputStream();
    ostream.write( strParams.getBytes() );
    ostream.flush();
    ostream.close();
    }
    catch( Exception e )
    {
    // failed to send data..
    // continue on, try to read in data
    }
    }

    // Now that we have (possibly) sent any necessary data *up* to the
    web
    // service, it's time to read the response from the server.
    // The data that comes down will be in the form of an encrypted
    string
    // of name value pairs in the same format as an HTTP querystring
    (i.e.,
    // name1=value1&name2=value2)

    // The string buffer we'll use to build up the response
    StringBuffer sb = new StringBuffer("");

    int iBytesRead = 0;
    int iBufferLen = 128;

    char[] rgc = null;
    try
    {
    InputStream istream = urlc.getInputStream();

    // Since we're reading in data from a remote source, we can't
    // count on it all being available instantly. We'll read it
    // into a buffer and read from the buffer
    BufferedReader br = new BufferedReader( new InputStreamReader(
    istream ) );

    while( br.ready() || iBytesRead != -1 )
    {
    // Create our character buffer
    rgc = new char[ iBufferLen ];
    // Read the data into the buffer.
    iBytesRead = br.read( rgc, 0, iBufferLen );
    // Append the data onto our StringBuffer
    if( iBytesRead > 0 )
    sb.append( new String( rgc ).trim() );
    }

    // Close the BufferedReader
    br.close();
    // Close the stream
    istream.close();
    }
    catch( Exception e )
    {
    // failed to read in response data
    return;
    }

    // Create a string from the StringBuffer object.
    strResponse = sb.toString();
    System.out.println( strResponse );
    }
    catch( Exception e )
    {
    }

    urlc = null;
    }
     
    Brock Jones, Sep 4, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Mueller
    Replies:
    5
    Views:
    526
    Steven Saunderson
    Jul 10, 2006
  2. Replies:
    2
    Views:
    3,608
  3. nono752
    Replies:
    0
    Views:
    162
    nono752
    Jul 11, 2003
  4. nono752
    Replies:
    0
    Views:
    127
    nono752
    Jul 11, 2003
  5. Bruce Lee

    WebService, Rational Robot and the FileNotFoundException

    Bruce Lee, Jun 11, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    137
    Bruce Lee
    Jun 11, 2004
Loading...

Share This Page