Discuss this help topic in SecureBlackbox Forum
Use of SecureBlackbox-powered SBBHttpWebRequest client
To use enhanced SecureBlackbox-based SBBHttpWebRequest client without affecting the rest of your application, create the instance of the WebRequest and use it explicitly:
C#:
SBUtils.Unit.SetLicenseKey("license key"); // you may set the key anywhere in your application
WebRequest request = SBBHttpTransportManager.CreateRequest("http://domain.com/");
WebResponse response = request.GetResponse();
To change the way your application works globally, you need to replace .NET's HTTP client with SecureBlackbox-based one. Below you will find the examples of how to perform various kinds of requests using SBBHttpWebRequest.
Simple GET request
C#:
SBUtils.Unit.SetLicenseKey("license key"); // you may set the key anywhere in your application
SBBTransportManager.RegisterTransport(); // register SBB transport
WebRequest request = WebRequest.Create("http://domain.com");
WebResponse response = request.GetResponse();
WebResponse response = request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
// read the response and process it
string body = sr.ReadToEnd();
Process(body);
}
response.Close();
SBBTransportManager.UnregisterTransport(); // unregister transport (optional)
GET request with user authentication
C#:
SBUtils.Unit.SetLicenseKey("license key"); // you may set the key anywhere in your application
SBBHttpTransportManager.RegisterTransport();
CredentialCache credCache = new CredentialCache();
credCache.Add(new Uri("http://httpbin.org/"), "Basic", new NetworkCredential("user", "passwd"));
// "Digest" method is also supported
WebRequest request = WebRequest.Create("http://httpbin.org/basic-auth/user/passwd");
request.Credentials = credCache;
request.PreAuthenticate = true;
Stream dataStream = response.GetResponseStream();
HTTPS request with certificate validation
C#:
SBUtils.Unit.SetLicenseKey("license key"); // you may set the key anywhere in your application
// single validator can be used for multiple requests
private TElX509CertificateValidator CertificateValidator = new TElX509CertificateValidator();
//...
SBBHttpTransportManager.RegisterTransport();
Uri url = new Uri("https://google.com");
WebRequest request = WebRequest.Create(url);
(request as SBBHttpWebRequest).Versions = SBSSLConstants.Unit.sbTLS12 |
SBSSLConstants.Unit.sbTLS11 | SBSSLConstants.Unit.sbTLS10;
(request as SBBHttpWebRequest).OnCertificateValidate +=
new TSBCertificateValidateEvent(DoCertificateValidate);
WebResponse response = request.GetResponse();
//...
// validation callback
private void DoCertificateValidate(object Sender, TElX509Certificate X509Certificate,
ref TSBCertificateValidity Validity, ref int Reason)
{
SBBHttpWebRequest req = (SBBHttpWebRequest)Sender;
string url = req.RequestUri.Host; // request URL
// validate certificate
if ((X509Certificate.Chain == null) || (X509Certificate.Chain.get_Certificates(0) == X509Certificate))
{
// For proper CRL and OCSP validation please read instructions in
// description of TElX509CertificateValidator class in the help file
CertificateValidator.ValidateForSSL(X509Certificate, url,
null, TSBHostRole.hrServer, null, false, DateTime.Now,
ref Validity, ref Reason);
}
else
Validity = TSBCertificateValidity.cvOk;
}
Post request sample
C#:
SBUtils.Unit.SetLicenseKey("license key"); // you may set the key anywhere in your application
SBBHttpTransportManager.RegisterTransport();
WebRequest request = WebRequest.Create(url);
request.Method = "POST";
byte[] postData = Encoding.UTF8.GetBytes("parameter1=value1¶meter2=value2");
Stream reqStream = request.GetRequestStream();
reqStream.Write(postData, 0, postData.Length);
reqStream.Close();
WebResponse response = request.GetResponse();
//...