2

我有一个需要安全证书的 cURL 命令。我正在尝试用 C# 中的 WebRequest 替换调用。

这是 curl 命令:

curl -d "uid=UUUUUU&password=PPPPPP&active=Y&type=I" https://www.aidap.naimes.faa.gov/aidap/XmlNotamServlet --key aidap_key.pem --cacertaidap_ca.pem --certaidap_client.pem:开始123 -k -O

我被困的地方是如何合并--key、--cacert 和--cert 参数。目前我正在设置 WebRequest.PreAuthenticate=true 并使用我的证书存储中的安全证书的名称和密码创建 NetworkCredential(我假设在 --key、--cacert 等中)。

当我运行下面的代码时,我收到了 403 禁止异常。

有任何想法吗?

这是我正在使用的代码:

 public void RetrieveNotams()
        {
            string myURL = "https://www.aidap.naimes.faa.gov/aidap/XmlNotamServlet";

            HttpWebRequest req;

            NetworkCredential myCred = new NetworkCredential("AAAAA", "BBBBB");

            req = (HttpWebRequest)WebRequest.Create(myURL);

            req.PreAuthenticate = true;

            req.Credentials = myCred;

            req.Method = "POST";

            req.ContentType = "application/x-www-form-urlencoded";
            string postData = "uid=UUUUUU&password=PPPPPP&active=Y&type=I";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            req.ContentLength = byteArray.Length;

            using (Stream dataStream = req.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();
            }

            req.GetResponse();

        }
4

2 回答 2

2

我在

http://www.codeproject.com/Articles/28395/Attaching-a-digital-certificate-public-key-to-an-H

我从证书存储中获取证书,并将其添加到请求的证书集合中。并取出 PreAuthenticate 代码。

这是更新的代码:

public void RetrieveNotams()
{
    string myURL = "https://www.example.com/SecureSite";

    // Open the certificate store for the current user in readonly mode,
    // and find the certificate I need to user
    X509Store xstore = new X509Store(StoreLocation.CurrentUser);

    xstore.Open(OpenFlags.ReadOnly);

    X509Certificate cert=null; 

    foreach (X509Certificate c in xstore.Certificates)
        if(c.Subject.StartsWith("CN=aidapuser"))
            cert=c;          

    HttpWebRequest req;

    req = (HttpWebRequest)WebRequest.Create(myURL);

    // add the certificate to the request
    req.ClientCertificates.Add(cert);

    req.Method = "POST";

    req.ContentType = "application/x-www-form-urlencoded";
    string postData = "uid=UUUUUUU&password=PPPPPP&active=Y&type=I";
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    req.ContentLength = byteArray.Length;

    // add the parameters to POST to the URL
    using (Stream dataStream = req.GetRequestStream())
    {
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
    }

    // grab the response and show the first 10 lines

    using (StreamReader sr = new StreamReader(req.GetResponse().GetResponseStream()))
    {

        for (int i = 0; i < 10; i++)
            Console.WriteLine(sr.ReadLine());
    }

}
于 2012-11-23T18:52:51.107 回答
0
        var request_object = new stdClass();
        request_object.name = Encoding.UTF8.GetBytes("Shipping Sys");
        request_object.no = 999;
        request_object.staff = Encoding.UTF8.GetBytes("shimizu@ultimatrust.co.jp");

        string jsonBody3 = "{\"name\":\"Shipping Sys\",\"no\":999,\"staff\":\"shimizu@ultimatrust.co.jp\"}";
        string jsonBody4 = "name=ShippingSys&no=999&staff=shimizu@ultimatrust.co.jp";
        int lenghth = jsonBody4.Length;
     
        string URL = "https://stage.ai-shot.com/shibano/Shipping/get_token.php";
        System.Net.HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(URL);

        X509Store xstore = new X509Store(StoreLocation.CurrentUser);
        xstore.Open(OpenFlags.ReadOnly);
        X509Certificate cert2 = null;
        foreach (X509Certificate c in xstore.Certificates)
            if (c.Subject.StartsWith("CN=GlobalSign Root CA"))
                cert2 = c;
        // add the certificate to the request
        httpWebRequest.ClientCertificates.Add(cert2);

        byte[] arrayData = Encoding.UTF8.GetBytes(jsonBody4);
        ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault | SecurityProtocolType.Tls12;

        httpWebRequest.Method = "POST";
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.ContentLength = arrayData.Length;


        CredentialCache credentialCache = new CredentialCache();

        NetworkCredential myCred = new NetworkCredential("shimizu@ultimatrust.co.jp", "fugafuga");

        httpWebRequest.Credentials = myCred;
        httpWebRequest.PreAuthenticate = true;

        using (System.IO.Stream stream = httpWebRequest.GetRequestStream())
        {
            stream.Write(arrayData, 0, arrayData.Length);
            stream.Close();
        }

        HttpWebResponse res = httpWebRequest.GetResponse() as HttpWebResponse;
于 2021-12-09T03:18:03.463 回答