11-11-2011 06:36 AM - edited 11-11-2011 06:41 AM
I tried to access a HTTPS connection by entering the value in browser, it seems to work fine and redirects me to expected page/output. But when I tried the same using the code, I am unable to get the result. I tried of setting the UserAgent as (Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en-GB) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.141 Mobile Safari/534.1+). But no luck. Can anyone help with this to work right? Thanks in Advance.
11-11-2011 06:47 AM
11-11-2011 07:20 AM
Thanks for reply simon_hain
Here is my code. I am getting trusted connection alert, when i click continue i get response code 302.How can i implement secure connection certificate to disable trusted connection alert.
try{
String SvrUrl="",Url,ErrorResponse,HTMLText;
HttpConnection conn,newhttpConn;
TransportDetective _td = new TransportDetective();
int _availableTransports = 0;
_availableTransports = _td.getAvailableTransportCoverage();
if(!DeviceInfo.isSimulator()){
Url = getFullURL(SvrUrl+";deviceside=true", _availableTransports);
}else{
Url = SvrUrl;
}
conn = (HttpConnection) Connector.open(Url, Connector.READ_WRITE);
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Accept","text/html,applic ation/xml,application/xhtml+xml,text/html;q=0.9,te xt/plain;q=0.8,image/png,;q=0.5");
conn.setRequestProperty(HttpHeaders.HEADER_ACCEPT_ CHARSET, "UTF-8");
conn.setRequestProperty("User-Agent",System.getPro perty("browser.useragent")); System.out.println("browser.useragent"+System.getP roperty("browser.useragent"));
URLEncodedPostData oPostData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHAR SET, false);
oPostData.append("User-Agent",System.getProperty(" browser.useragent"));
OutputStream finalOut = conn.openOutputStream();
finalOut.write(oPostData.getBytes());
finalOut.flush();
finalOut.close();
int rc = conn.getResponseCode();
System.out.println("rc=="+rc);
if (rc == HttpConnection.HTTP_OK)
{
//status = true;
}
InputStream Resinput = conn.openInputStream();
byte[] data = new byte[300240];
int len = 0;
int size = 0;
StringBuffer raw = new StringBuffer();
while ( -1 != (len = Resinput.read(data)) )
{
raw.append(new String(data, 0, len));
size += len;
}
HTMLText = raw.toString();
System.out.println("HTMLText==="+HTMLText);
if( rc == HttpConnection.HTTP_TEMP_REDIRECT
|| rc == HttpConnection.HTTP_MOVED_TEMP
|| rc == HttpConnection.HTTP_MOVED_PERM ) {
String location = conn.getHeaderField("location").trim();
System.out.println("location========"+location);
try
{
_availableTransports = _td.getAvailableTransportCoverage();
if(!DeviceInfo.isSimulator()){
Url = getFullURL(location+";deviceside=true", _availableTransports);
}else{
Url = location;
}
newhttpConn = (HttpConnection) Connector.open(Url, Connector.READ_WRITE);
newhttpConn.setRequestMethod(HttpConnection.POST);
newhttpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
newhttpConn.setRequestProperty("Accept","text/html ,application/xml,application/xhtml+xml,text/html;q =0.9,text/plain;q=0.8,image/png,;q=0.5");
newhttpConn.setRequestProperty(HttpHeaders.HEADER_ ACCEPT_CHARSET, "UTF-8");
URLEncodedPostData oPostData1 = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHAR SET, false);
oPostData1.append("User-Agent",System.getProperty( "browser.useragent"));
OutputStream finalOut1 = newhttpConn.openOutputStream();
finalOut1.write(oPostData.getBytes());
finalOut1.flush();
finalOut1.close();
int rc1 = newhttpConn.getResponseCode();
System.out.println("rc1------"+rc1);
if (rc1 == HttpConnection.HTTP_OK)
{
//status = true;
}
InputStream Resinput1 = newhttpConn.openInputStream();
byte[] data1 = new byte[300240];
int len1 = 0;
int size1 = 0;
StringBuffer raw1 = new StringBuffer();
while ( -1 != (len = Resinput1.read(data1)) )
{
raw1.append(new String(data1, 0, len1));
size1 += len1;
}
String strResponse = raw1.toString();
System.out.println("strResponse========"+strRespon se);
}
catch (Exception e)
{
System.out.println( e.toString());
}
finally {
try {
if ( newhttpConn != null ) {
newhttpConn.close();
}
} catch (Exception e) {
}
newhttpConn = null;
}
}
}
catch (Exception e)
{
ErrorResponse = "Exception: " + e.toString();
System.out.println(ErrorResponse);
}
finally {
try {
if ( conn != null ) {
conn.close();
}
} catch (Exception e) {
}
conn = null;
}
}