0

I need some images from a portal and they are only accessible if I login to the portal.

I need to do it with a C# program. I don't know what username field and password field are, because they use POST method. After loging in I want to enter some URLs that contain the images I want.

What should I do?

For logging in I'm using:

 HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(@"http://mysite.com");

            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = "UsernameFieldName=Something";
            postData += "&PasswordFieldName=SomethingElse";
            byte[] data = encoding.GetBytes(postData);

            httpWReq.Method = "POST";
            httpWReq.ContentType = "application/x-www-form-urlencoded";
            httpWReq.ContentLength = data.Length;

            using (Stream stream = httpWReq.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();

            string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

Then for downloading image which is in another page I use:

 using (var client = new WebClient())
            {
                string FileName = @"image.jpg";
                client.DownloadFile("http://mysite.com/Image?imgCode=12345", FileName);
            }
Payam Sh
  • 581
  • 4
  • 9
  • 21

1 Answers1

1

I don’t have a complete solution but here are some details to get you started.

  • Figure out what are post field simply by looking at page source

  • Once you send request for login you’ll also need to find a way to accept authentication cookie and then send it in all subsequent requests because their application most probably uses cookies.

After you are logged in you can download images like this

        string imageFile = @"c:\image.jpg";
        using (System.Net.WebClient client = new System.Net.WebClient())
        {
            client.DownloadFile("http://www.somewebsite.com/someimage.jpg", imageFile);
        }

Here are couple examples to get you started with http posts in C# HTTP request with post

Community
  • 1
  • 1
Dwoolk
  • 1,491
  • 13
  • 8