c#.Net4 webRequest over SSL无法正常工作

时间:2010-09-14 20:02:00

标签: c# .net-4.0 https

嗨,这可以在发布到http地址时有效,但在发布到无法建立信任关系的HTTPS地址时失败!

我需要做些什么,或者这是服务器错误!?

private static string HttpPost (string uri, string parameters)
{
    //return "ok";
    // parameters: name1=value1&name2=value2    
    try
    {
        WebRequest webRequest = WebRequest.Create(uri);

        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.Method = "POST";
        byte[] bytes = Encoding.ASCII.GetBytes(parameters);
        Stream os = null;
        try
        { // send the Post
            webRequest.ContentLength = bytes.Length;   //Count bytes to send
            os = webRequest.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);         //Send it
        }
        catch (WebException ex)
        {
            MessageBox.Show(ex.Message, "HttpPost: Request error",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            if (os != null)
            {
                os.Close();
            }
        }

        try
        { // get the response
            WebResponse webResponse = webRequest.GetResponse();
            if (webResponse == null)
            { return null; }
            StreamReader sr = new StreamReader(webResponse.GetResponseStream());
            return sr.ReadToEnd().Trim();
        }
        catch (WebException ex)
        {
            MessageBox.Show(ex.Message, "HttpPost: Response error",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    catch
    {

    }
   return null;
} // end HttpPost 

2 个答案:

答案 0 :(得分:1)

SSL证书已过期或无效。在服务器上部署新的有效SSL证书。

答案 1 :(得分:1)

如果这是正确的事情,请明确信任证书。