Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 37
Registered: ‎07-23-2011
My Device: 9700
My Carrier: Airtel

User-Agent

[ Edited ]

 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.

Developer
Posts: 17,012
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: User-Agent

what is your code? do you handle redirects?
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 37
Registered: ‎07-23-2011
My Device: 9700
My Carrier: Airtel

Re: User-Agent

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,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,;q=0.5");
        conn.setRequestProperty(HttpHeaders.HEADER_ACCEPT_CHARSET, "UTF-8");   
		conn.setRequestProperty("User-Agent",System.getProperty("browser.useragent"));                 System.out.println("browser.useragent"+System.getProperty("browser.useragent"));

        URLEncodedPostData oPostData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, 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_CHARSET, 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========"+strResponse);
            }
            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;
    }  
}