Java从HTTPS html表单下载文件

Java从HTTPS html表单下载文件,java,httpurlconnection,Java,Httpurlconnection,我必须从网站上下载一个文件。这是一个普通的html表单,我通常从html表单下载几个文件。但这个网站只允许通过HTTPS访问 我有一个很棒的程序,我不能使用ApacheCommonsHttpClient,因为除了excel和PDF文件之外,我不能从我的工作中下载任何东西。很难得到月食 因此,我使用的是HttpURLConnection(我也尝试了HttpsURLConnection),但这样我甚至无法连接到站点,因此无法向表单发送参数并下载文件 拜托,有人能帮我吗 感谢和问候。您应该使用Http

我必须从网站上下载一个文件。这是一个普通的html表单,我通常从html表单下载几个文件。但这个网站只允许通过HTTPS访问

我有一个很棒的程序,我不能使用ApacheCommonsHttpClient,因为除了excel和PDF文件之外,我不能从我的工作中下载任何东西。很难得到月食

因此,我使用的是HttpURLConnection(我也尝试了HttpsURLConnection),但这样我甚至无法连接到站点,因此无法向表单发送参数并下载文件

拜托,有人能帮我吗


感谢和问候。

您应该使用
HttpsURLConnection
并验证请求

看看这里:

编辑

应该类似于以下代码:

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;
    }

谢谢你的回复。您传递的链接没有解释如何进行身份验证。你能告诉我正确的一个吗?我希望它有帮助:连接总是返回以下错误:
连接超时:连接已编辑,但“连接超时”似乎与它已加密的事实无关,如果你在身份验证方面有问题,它会写一些类似“身份验证失败…”的内容,也许你的代码中有链接或其他东西。再次感谢你。你写的都是C语言,我用的是Java。我不知道如何翻译它。我认为您对其工作方式的看法是正确的,可能与身份验证无关。我真的不知道。