problem in htttp connection
Please Login to Remove!
I am doing http connection in application. My MDS is working wll in my JDE 4.0 but yet it is giving wrong response code like 406 but it is working on my client pc when he has installed JDE 4.2 and visual studio but what i am thinking is what could be wrong in JDE .0
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;
System.out.println(url);
String credentials = Credentials();
System.out.println(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();
System.out.println("Response code="+rc);
if (rc != HttpConnection.HTTP_OK) {
throw new IOException("HTTP response code: " + rc);
}
is = c.openInputStream();
System.out.println("after in stream");
// Get the ContentType
String type = c.getType();
System.out.println("after in stream");
// Get the length and process the data
int len2 = (int)c.getLength();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
System.out.println("after in stream");
//byte[] buffer = new byte[256];
byte[] buffer = new byte[len2];
System.out.println("after in stream");
is.read(buffer);
byteArrayOutputStream.write(buffer);
/*
int len = 0, imageSize = 0;
while (-1 != (len = is.read(buffer))) {
byteArrayOutputStream.write(buffer);
imageSize += len;
}
*/
byteArrayOutputStream.flush();
System.out.println("after in stream");
String result = byteArrayOutputStream.toString();
System.out.println("after in stream="+result);
byteArrayOutputStream.close();
return result;
} catch (ClassCastException e) {
throw new IllegalArgumentException("Not an HTTP URL");
} catch (IOException e) {
System.out.println(e.toString());
throw new IllegalArgumentException("Not an HTTP URL");
}
catch(Exception e)
{
System.out.println(e.toString());
}
finally
{
}
return null;
}
The following is my code for http.
__________________
Kartik Trivedi
(Sun Certified Java Developer)
(Forum Nokia Champion)
(Software Developer-J2ME)
(Saltriver Infosystem)
|