Quote:
Originally Posted by Ananthasivan V K Please paste the code segment that you used, what are you doing inside it ? |
The following is code. take a look at it and plesae let me know if you find any thing wrong in it.
private String MakeGetRequest(String command)
{
//open the connection and extract the data
StreamConnection s = null;
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
int rc;
String url = Url() + command;
String credentials = Credentials();
try {
c = (HttpConnection)Connector.open(url);
c.setRequestMethod(HttpConnection.GET);
c.setRequestProperty("Connection", "Keep-Alive");
c.setRequestProperty("User-Agent", "test app");
c.setRequestProperty("Accept", "application/xml");
c.setRequestProperty("Content-Type", "application/xml");
c.setRequestProperty("Authorization", "Basic " + credentials);
os = c.openOutputStream();
// Getting the response code will open the connection,
// send the request, and read the HTTP response headers.
// The headers are stored until requested.
rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
throw new IOException("HTTP response code: " + rc);
}
is = c.openInputStream();
// Get the ContentType
String type = c.getType();
// Get the length and process the data
int len2 = (int)c.getLength();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//byte[] buffer = new byte[256];
byte[] buffer = new byte[len2];
is.read(buffer);
byteArrayOutputStream.write(buffer);
/*
int len = 0, imageSize = 0;
while (-1 != (len = is.read(buffer))) {
byteArrayOutputStream.write(buffer);
imageSize += len;
}
*/
byteArrayOutputStream.flush();
String result = byteArrayOutputStream.toString();
byteArrayOutputStream.close();
return result;
} catch (ClassCastException e) {
throw new IllegalArgumentException("Not an HTTP URL");
} catch (IOException e) {
throw new IllegalArgumentException("Not an HTTP URL");
} finally {
}
}