忽略SSL证书错误会导致WebRequest状态406

时间:2016-07-28 12:03:24

标签: c# ssl request httprequest

我目前正在编写自己的API测试应用程序,它有一个自己的测试应用程序,工作得很好。

它使用System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };// Allow all SSL peer certificates (for testing only!)

忽略证书错误

当我运行他们的测试应用程序时,它连接并注册就好了。但是,当我尝试连接我的应用程序时,我得到响应代码406.问题是,两个应用程序几乎相同。

唯一真正的区别是,我没有提供用于提供连接所需数据的GUI,因为我对它们进行了硬编码(仅用于测试)。我正在调用相同的方法,使用连接按钮并获取此错误代码。

我在我旁边有API的来源,但我不允许发布它。相反,我可以发送导致错误代码的方法:

public WWSVCC_Response RawHttpRequestBytes(string method, string path, WebHeaderCollection headers, dynamic body)
    {
        if (body != null && (body as byte[]) == null && (body as Dictionary<string, dynamic>) == null) throw (new Exception("Type of parameter \"body\" is invalid"));
        if (body != null && method == "GET") method = "PUT";
        byte[] bodyBytes = null;
        WebResponse res = null;
        try
        {
            string uri = ((SSL) ? "https" : "http") + "://" + Host + ":" + Port.ToString() + RootPath + path;
            WebRequest req = WebRequest.Create(uri);
            req.Headers = headers;
            ((HttpWebRequest)req).UserAgent = "WWSVC .NET client";
            ((HttpWebRequest)req).Accept = "*/*";
            req.Method = method;
            req.Timeout = 10000;
            Stream rs;
            if (body != null)
            {
                bodyBytes = body as byte[];
                if (bodyBytes == null) bodyBytes = JsonObjectEncoder(body);
                req.ContentLength = bodyBytes.Length;
                req.ContentType = "application/json";
                rs = req.GetRequestStream();
                rs.Write(bodyBytes, 0, bodyBytes.Length);
                rs.Close();
            }
            try
            {
                res = req.GetResponse();
            }
            catch (WebException ex)
            {
                res = ex.Response;
                System.Diagnostics.Trace.WriteLine(ex.ToString());
            }
            rs = res.GetResponseStream();
            byte[] resBytes;
            using (MemoryStream ms = new MemoryStream())
            {
                int red = 1;
                byte[] buffer = new byte[4096];
                while (red > 0)
                {
                    red = rs.Read(buffer, 0, buffer.Length);
                    if (red > 0) ms.Write(buffer, 0, red);
                }
                resBytes = ms.ToArray();
            }
            rs.Close();
            WWSVCC_Response r = new WWSVCC_Response(this, (int)((HttpWebResponse)res).StatusCode, resBytes, null, null, headers, bodyBytes);
            LastResponse = r;
            return r;
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
            WWSVCC_Response r = new WWSVCC_Response(this, (res == null) ? 0 : (int)((HttpWebResponse)res).StatusCode, null, null, ex, headers, bodyBytes);
            LastResponse = r;
            return r;
        }
    }

此时,我尝试了一切,我知道这可行。由于服务器应用程序是具有无效SSL证书的测试服务器,因此禁用“忽略证书”解决方法会导致请求失败。服务器应用程序仅适用于SSL,因为它可以使用可靠的数据。

你有什么想法,问题是什么?

0 个答案:

没有答案