How to use credentials in an HttpWebRequest

2

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

  

link {user} & pwd = {password}

Below is code working from the first template.

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 the credentials when returning an image, as shown in the image below, tested from a browser.

TherequestedCGIcommandfollows:

  

link

BelowisthedeadcodeforthesecondtestedIPcameramodel.

Urigeturi=newUri(_cgi);stringusername="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 someone has some other method to add credentials and wants to submit, you're welcome to try out.

EDIT 1

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

  

Error: ConnectFailure (Connection timed out)

Being that, via internet browser I have normal access and the tested ip usually drips in the camera.

    
asked by anonymous 06.04.2018 / 20:48

0 answers