繁体   English   中英

如何在.net中将Curl命令转换为HttpWebRequest?

[英]How to convert Curl command to HttpWebRequest in .net?

您好,我正在尝试使用vs2010 .net使用第3方Rest服务,这是cURL命令的示例,用于从该服务获取一些数据:

curl -k --header "X-Authorization: authorizationString" -G -X GET -d 'message' https://WebsiteAddress.com/api/command/914
  1. 如何设置参数-G -X -GET?
  2. 如何将-H标头参数更改为--header? 还是我必须这样做?

这是我到目前为止的内容:

string authorizationString = "bla bla";
string message = "my Message";
string url = "https://WebsiteAddress.com/api/command/914";
var req = (HttpWebRequest)WebRequest.Create(url);
req.ContentType = "application/json";
req.Method = "Get";
req.Headers.Add("X-Authorization", authorizationString);

//bypassing untrusted certificate 
//if DUBUG
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
//end DEBUG

using (var PostData = new StreamWriter(req.GetRequestStream()))
{
    PostData.Write(message);
    PostData.Flush();
}

var response = (HttpWebResponse)req.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
    //TO DO:
}

应该是这样的:

using(WebClient webClient = new WebClient())
{
    webClient.Headers.Add("X-Authorization", "authorizationString");
    string response = webClient.DownloadString("https://WebsiteAddress.com/api/command/914?message");
}

阅读此内容: curl.haxx.se/docs/manpage.html
您正在尝试建立连接,HTTP GET INSECURE WITH EXTRA HEADER和'message'将连接到您的URL。

暂无
暂无

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

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