Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > URLConnection to WebService gets FileNotFoundException

Reply
Thread Tools

URLConnection to WebService gets FileNotFoundException

 
 
Brock Jones
Guest
Posts: n/a
 
      09-04-2003
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;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OutputStream from a URLConnection produces an OutOfMemory OutputStream from a URLConnection produces an OutOfMemory WinstonSmith_101@hotmail.com Java 2 10-25-2006 04:45 PM
Not only the selected HREF gets surrounded, but the whole row gets surrounded Stefan Mueller HTML 5 07-10-2006 11:53 AM
WebService, Rational Robot and the FileNotFoundException Bruce Lee ASP .Net Web Services 0 06-11-2004 06:39 AM
runtime error : FileNotFoundException on webservice call nono752 ASP .Net Web Services 0 07-11-2003 01:11 PM
runtime error : FileNotFoundException on webservice call nono752 ASP .Net Security 0 07-11-2003 01:09 PM



Advertisments