0

I have to download a file from a website. It's a normal html form, I usually download several files from html forms. But this one is in a site that only allow access via HTTPS.

I have one great program, I can't use Apache Commons HttpClient, because I can't download nothing from my job beyond excel and pdfs files. It was very hard to get Eclipse.

Thus, I'm using HttpURLConnection (I tried HttpsURLConnection too), but this way I can't even connect to the site, so send parameters to the form and download the file are been impossible.

Please, could someone help me?

Thanks and regards.

Thiago Melo
  • 1,157
  • 1
  • 14
  • 31

1 Answers1

0

Well you should use HttpsURLConnection and authenticate the request.

look into here: Tring to connect using HTTPS: Server redirected too many times

Edit

Should be something like this code:

private void SendRequest(string url, string data){

    Stream dataStream = null;
    WebResponse response = null;

    try
    {
        string requestXml = Sendingxml.ToString();

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";

        ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);

        string formParams = string.Format("data={0}",  data);

        byte[] byteArray = Encoding.UTF8.GetBytes(formParams);
        request.ContentType = "application/x-www-form-urlencoded";
        encoding='utf-8'";
        request.ContentLength = byteArray.Length;

        dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        response = request.GetResponse();
        dataStream = response.GetResponseStream();

        string responseFromServer = "";
        using (StreamReader reader = new StreamReader(dataStream))
        {
            responseFromServer = reader.ReadToEnd();
        }

        return responseFromServer;
    }
    catch (Exception e)
    {
        throw new CommunicationFailure();
    }
    finally
    {
        if (dataStream != null)
            dataStream.Close();
        if (response != null)
            response.Close();
    }
}


private bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
    {
        return true;
    }
Community
  • 1
  • 1
omriman12
  • 1,644
  • 7
  • 25
  • 48
  • Thank you about your response. The link you passed doesn't explain how to authenticate. Could you please let me know the right one? – Thiago Melo Apr 01 '14 at 16:59
  • I hope it helps: the connection always return the folloing erro: `Connection timed out: connect` – Thiago Melo Apr 01 '14 at 17:05
  • Edited, but 'connection timed out' doesnt seem to be connected to the fact that its encrypted, if you had a problem with authentication it would have writen something like 'Failed to authenticate..', maybe something with the link or something else in your code. – omriman12 Apr 02 '14 at 08:10
  • Thank you again. All you wrote is in C#, I'm using Java. I'm not knowing how to translate it. I think you are right about the way it works and might be that has nothing with authentication. I really don't know. – Thiago Melo Apr 02 '14 at 18:26