0 votes
by (120 points)

I am trying to use Https library for Digest Authentication to post Json data. Can anyone have a sample for this. Thanks in advance

Applies to: Rebex HTTPS

1 Answer

0 votes
by (62.7k points)

Digest Authentication is performed automatically, if requested by the server. You only need to set the Credentials property on the WebClient or HttpRequest.

You can test it against httpbin.org like this:

using (var client = new Rebex.Net.WebClient())
    client.Credentials = new NetworkCredential("usr", "pwd");
    var authTest = client.DownloadString("http://httpbin.org/digest-auth/auth/usr/pwd");

Unfortunately, httpbin.org does not support POST for digest-auth testing. However, it would work the same way, just instead DownloadString use the UploadString method.

If the UploadString method does not work with your server and you get for example 400 Bad Request. response, your server probably requires Content-Type header to be specified. In such case, you cannot use simple WebClient.UploadString() method, but you need to do it using HttpRequst API.

You can test it against reqres.in like this:

var creator = new HttpRequestCreator();

var req = (WebRequest)creator.Create("https://reqres.in/api/login");
req.Method = "POST";
req.Headers.Add("Accept", "application/json");
req.Headers.Add("Content-Type", "application/json");
req.Credentials = new NetworkCredential("eve.holt", "pwd");
using (var upload = new StreamWriter(req.GetRequestStream()))
    var data = @"{""email"": ""eve.holt@reqres.in"", ""password"": ""pwd""}";

var res = req.GetResponse();
using (var download = new StreamReader(res.GetResponseStream()))
    var response = download.ReadToEnd();
by (120 points)
Thank You.
I have tried the second code and I am getting the error "Insufficient response."
I am using this to send sms via dinstar sms gateway.
My url is like https://example.com/api/send_sms
I am using like this
var req = (WebRequest)creator.Create("https://example.com/api/send_sms");

Is this correct?
by (62.7k points)
Yes, you are doing it correctly.
Can you please create VERBOSE communication log and post it here or send it to support@rebex.net for analysis. It can be created like this:

    var creator = new HttpRequestCreator();
    creator.LogWriter = new Rebex.FileLogWriter(@"c:\data\http.log", Rebex.LogLevel.Verbose);
by (120 points)
Yes. I have sent the log to support@rebex.net
by (62.7k points)
edited by
The log showed that the server is violating HTTP protocol by not including any info for HTTP response payload. It means there is no Content-Length nor Transfer-Encoding nor Connection:close HTTP header.

In such case the client cannot read the response, because it does not know where the response ends. This resulted to "Insufficient response." error.

We realized that the system WebClient is able to workaround this situation if the HTTP response code is "401 Unauthorized".

If you need the similar kind of workaround in Rebex HTTPS please let us know.