0

URL: http://monSite/GET.asp

we must authenticate before getting the result.

I want to send the login and password with HttpConn.setRequestMethod (HttpConnection.POST) and retrieve the XML file with HttpConn.setRequestMethod (HttpConnection.GET) with the same HTTP Client.

conn = (HttpConnection) new ConnectionFactory().getConnection(_url).getConnection();
URLEncodedPostData postData = null;
postData = new URLEncodedPostData("UTF-8", false);

postData.append("userName",_username);
postData.append("passWord", _password);


conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LANGUAGE, "en-US");

conn.setRequestProperty(HttpProtocolConstants.HEADER_CACHE_CONTROL,"no-cache, no-store, no-transform");


// Specify the content type.
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, postData.getContentType());

byte [] postBytes = postData.getBytes();
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer.toString(postBytes.length));
os = conn.openOutputStream();

os.write(postBytes);

os.flush();
os.close();
//GET XML file
conn.setRequestMethod(HttpConnection.GET);
conn.setRequestProperty("User-Agent",
        "Profile/MIDP-1.0 Confirguration/CLDC-1.0");

if (conn.getResponseCode() == HttpConnection.HTTP_OK) {
int total = 0;
int size = 1024;
char[] buffer = new char[size];
int len;

InputStreamReader isr = new InputStreamReader(conn.openInputStream(), "UTF-8");

while ((len = isr.read(buffer, 0, size)) > 0)
{
    buff.append(buffer, 0, len);
    total += len;
}



result = buff.toString();
} else {
    result = "Error in connection" + conn.getResponseCode();
}


} catch (Exception ex) {
    ex.printStackTrace();
} finally {
    try {
        if (in != null) {
            in.close();
        }
        conn.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

the HttpConnection.POST works very well but GET no (login failed: the authentication parameter does not saved)

rouge
  • 3
  • 2

1 Answers1

0

In HttpConnection.GET request you need to append attribute in your url like:

String url="http://www.xyz?userName="+_username+"&password="+_password+"";

and then get the InputStream

below link may be helpful for you

http://supportforums.blackberry.com/t5/Java-Development/Make-an-HTTP-Connection-to-get-a-Content-of-URL/td-p/95075

Vivek Kumar Srivastava
  • 2,158
  • 1
  • 16
  • 23
  • thx Vivek Kumar Srivastava for reply, but when i append attribute in my url, i receives 'login failed' not the XML file that i need :( we must use POST to LOG-IN and GET to receve the XML file – rouge Jul 12 '11 at 14:07
  • if I directly open http://monSite/GET.asp?userName=vivek&passWord=1234 on the desktop browser, then it also not work. If this url is incorrect then give the right url. And in your code no need to use conn.setRequestMethod(HttpConnection.GET); – Vivek Kumar Srivastava Jul 13 '11 at 04:46