How to use credentials on an Httpwebrequest

Asked

Viewed 302 times

2

I’m trying to capture an image from an IP camera. In the first model tested, it worked normally, since the model in test did not require credentials to return the images as a array de bytes because the credentials already came in the requested CGI command line, as follows:

http://192.168. 0.25:88/cgi-bin/Cgiproxy.fcgi? cmd=snapPicture2&usr={user}&pwd={password}

Below is the working code of the first model.

Uri geturi = new Uri(_cgi);
//Solicita um comando CGI para câmera FOSCAM
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(geturi);
byte[] imgByte = null;
//Pega a Resposta do comando CGI, neste comando uma imagem.
using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
   {
       if (response != null)
         {
            using (BinaryReader reader = new BinaryReader(response.GetResponseStream()))
               {
                  imgByte = reader.ReadBytes(1 * 640 * 480 * 10);
               }
          }
   }

For the new camera model, it asks for credentials when returning an image, as follows in the example below, tested from a browser.

Teste no navegador

Follow the requested CGI command:

http://{user}:{password}@192.168.0.29:80/Streaming/Channels/1/picture

Below is the dead code of the second tested IP camera model.

Uri geturi = new Uri(_cgi);
string username = "usuário";
string password = "senha";

//Seta as credenciais
NetworkCredential cred = new NetworkCredential(username, password);
CredentialCache cache = new CredentialCache();
cache.Add(geturi, "Basic", cred);

//Solicita um comando CGI para câmera FOSCAM
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(geturi);
request.PreAuthenticate = true;
request.Credentials = cache;
byte[] imgByte = null;

//Pega a Resposta do comando CGI, neste comando uma imagem.
using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
{
   if (response != null)
      {
         using (BinaryReader reader = new BinaryReader(response.GetResponseStream()))
            {
               imgByte = reader.ReadBytes(1 * 1024 * 1024 * 10);
               //Converte os Bytes para um bitmap.
               if (BytesToBitmap(imgByte) != null)
                  {
                      RunOnUiThread(() =>
                      {
                         _image.SetImageBitmap(BytesToBitmap(imgByte));
                      });
                      return true;
                  }
            }
            return false;
      }
      return false;
}

If anyone has any other method to add credentials and want to present, they are welcome to test.

EDIT 1

I changed the method cache.Add(geturi, "Basic", cred); for cache.Add(geturi, "NTLM", cred);. However, the two return the following message

Error: Connectfailure (Connection timed out)

Since, via internet browser I have normal access and the tested ip drips normally on camera.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.