Implement basic HTTP authentication

Description An application may need to connect to a resource that is protected using basic HTTP authentication. The following code sample illustrates how to authenticate using basic HTTP authentication from within a BlackBerry smartphone application. For a complete code sample showing how to create an HTTP connection (without authentication), see the httpdemo application included with the BlackBerry® Java® Development Environment. HttpConnection httpConn = null; StreamConnection s = null; boolean keepGoing = true; int dialogResponse; try { s = (StreamConnection)Connector.open("http://mysite.com/myProtectedFile.txt"); httpConn = (HttpConnection)s; while(keepGoing) { int status = httpConn.getResponseCode(); switch (status) { case (HttpConnection.HTTP_OK): //Connection is 200 OK. //Download and process data. keepGoing = false; break; case (HttpConnection.HTTP_UNAUTHORIZED): //Connection is 401 UnAuthorized. //A login and password is required. //Obtain the login information from somewhere. //You could prompt the user for this information or //retrieve this from elsewhere if it is saved within //your application. //Login information is hard coded here for brevity, but //we ask the user if they want to log in. UiApplication.getUiApplication().invokeAndWait(new Runnable() { public void run() { dialogResponse = Dialog.ask (Dialog.D_YES_NO,"Unauthorized Access:/n Do you wish to log in?"); } }); if (dialogResponse == Dialog.YES) { String login = "username:password"; //Close the connection. s.close(); //Encode the login information in Base64 format. byte[] encoded = Base64OutputStream.encode(login.getBytes(), 0, login.length(), false, false); //Open a new connection. s = (StreamConnection)Connector.open("http://mysite.com/myProtectedFile.txt "); httpConn = (HttpConnection)s; //Add the authorized header. httpConn.setRequestProperty("Authorization", "Basic " + new String(encoded)); } else { //Handle failed connection. keepGoing = false; } break; default: //The connection failed for some other reason. //Handle failed connection. keepGoing = false; break; } } //Close the connection. s.close(); } catch (IOException e) { //Handle the exception. }
Sure, here's an example of how to implement retry in Java for an HTTP request using the Apache HttpClient library: ```java import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.protocol.HttpContext; import java.io.IOException; public class HttpRequestRetry { private static final int MAX_RETRIES = 3; public static void main(String[] args) throws IOException { HttpClient httpClient = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet("https://www.example.com"); int retryCount = 0; boolean success = false; while (retryCount < MAX_RETRIES && !success) { try { httpClient.execute(httpGet, (HttpContext) null); success = true; } catch (IOException e) { System.out.println("Request failed, retrying..."); retryCount++; } } if (success) { System.out.println("Request succeeded after " + retryCount + " retries."); } else { System.out.println("Request failed after " + retryCount + " retries."); } } } ``` In this example, we create an `HttpClient` instance using the `HttpClientBuilder` class, and then create an `HttpGet` request object for the URL we want to retrieve. We then loop through the request up to a maximum number of retries (in this case, 3), attempting the request each time and catching any `IOException` that is thrown. If the request succeeds, we set the `success` flag to true and exit the loop. If the request fails after the maximum number of retries, we print a failure message.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值