简体   繁体   English

如何将curl命令移植到RestSharp? 如何排除故障?

[英]How to port a curl command to RestSharp? How to troubleshoot?

I have some working curl commands, to a web service, and now I want to move them to a C# program. 我在Web服务中有一些有效的curl命令,现在我想将它们移至C#程序。 I am using RestSharp, and trying with the simplest of the web service calls, but just keep getting a generic error message, and I am a bit stumped how to troubleshoot it. 我正在使用RestSharp,并尝试使用最简单的Web服务调用,但是一直收到一条通用错误消息,而我对如何解决它有些困惑。

Is there a way to see the headers, and exact URL, that is being sent, and the headers being received? 有没有办法查看发送的标题和确切的URL,以及接收的标题?

The curl example is basically this: curl示例基本上是这样的:

curl --user user:pw https://example.com/api/version

And my C# code is: 我的C#代码是:

var client = new RestClient("https://example.com");
client.Authenticator = new HttpBasicAuthenticator("user", "pw");
var request = new RestRequest ("api/version");
var response = client.Execute(request);
Console.WriteLine (response.Content);   
Console.WriteLine (response.StatusCode);    
Console.WriteLine (response.ErrorMessage);  

This gives me: 这给了我:

RestSharp.RestRequest

0
Error getting response stream (Write: The authentication or decryption has failed.): SendFailure

I am using Mono, on Linux. 我在Linux上使用Mono。 Would that be related? 那会相关吗? But I could find a few (more advanced) questions with the mono tag on StackOverflow, so it should work. 但是我可以在StackOverflow上找到带有mono标签的一些(更高级的)问题,因此它应该可以工作。 (?) (?)

If it was actually a problem with the username/password, I would get a 403 status, instead of a zero status, I assume? 如果实际上是用户名/密码有问题,我会假设状态为403,而不是零。

PS In case it matters, the rest of my script is: PS,如果它很重要,我的脚本其余部分是:

using System;
using System.Net;
using RestSharp;

namespace webtest
{
    class MainClass
    {
        public static void Main (string[] args)
        {
        ...(above code)
        }
    }
}

Regarding troubleshooting 关于故障排除

So far I can suggest: 到目前为止,我可以建议:

That was enough for me to see that http URLs work, https URLs fail. 这足以让我看到http URL有效,https URL失败。

(If you need more troubleshooting, and are using https, the sender parameter shown below contains various fields about the request being sent to the remote server.) (如果需要更多故障排除,并且正在使用https,则下面显示的sender参数包含有关发送到远程服务器的请求的各个字段。)

Regarding porting curl commands 关于移植curl命令

By default curl on linux uses the certificates it finds in /etc/ssl/certs . 缺省情况下,Linux上的curl使用它在/etc/ssl/certs找到的/etc/ssl/certs The blanket equivalent for Mono is to do mozroots --import --ask-remove , which will import all certificates (see Mono security FAQ ). Mono的等效方法是执行mozroots --import --ask-remove ,它将导入所有证书(请参见Mono安全性常见问题解答 )。

Another way to do it is by putting this at the very top of your program: 另一种方法是将其放在程序的顶部:

ServicePointManager.ServerCertificateValidationCallback +=
    (sender, certificate, chain, sslPolicyErrors) => {
    //Console.WriteLine(certificate.ToString());
    return true;
    };

The commented line can be used to report the certificate to the user, interactively get their approval, or to check the certificate fingerprint against the expected one. 注释行可用于向用户报告证书,以交互方式获得用户的认可,或将证书指纹与预期的指纹进行对比。 By simply returning true it means all certificates are trusted and unchecked. 简单地返回true意味着所有证书都是受信任的且未经检查。

Bonus: Cert checks 奖金:证书检查

Here is one way to check for a specific certificate: 这是检查特定证书的一种方法:

ServicePointManager.ServerCertificateValidationCallback +=
    (sender,certificate,chain,sslPolicyErrors) => {
    if(((System.Net.HttpWebRequest)sender).Host.EndsWith("google.com") ){
        if(certificate.GetCertHashString() == "83BD2426329B0B69892D227B27FD7FBFB08E3B5E"){
            return true;
        }
        Console.WriteLine("Uh-oh, google.com cert fingerprint ({0}) is unexpected. Cannot continue.",certificate.GetCertHashString());
        return false;
    }
Console.WriteLine("Unexpected SSL host, not continuing.");
return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM