简体   繁体   English

无法将一段CURL转换为C#

[英]Trouble converting a piece of CURL to C#

I have the following CURL that I am trying to convert to C#. 我尝试将以下CURL转换为C#。 I have no CURL experience whatsoever. 我没有任何CURL经验。

$ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL            => 'https://api.lanoba.com/authenticate',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST           => true,
        CURLOPT_POSTFIELDS     => array(
            'token'      => $_POST['token'],
            'api_secret' => 'YOUR-API-SECRET'
        )
    ));

Thus far I have come up with this: 到目前为止,我已经提出了这一点:

//Object to create a JSON object
public class LanobaJSONObject
{
    public string token { get; set; }
    public string api_secret { get; set; }
}

public void DoAuthenticationCheck()
{


    var token = Request["token"].ToString();

        var jsonObject = new LanobaJSONObject()
        {
            token = token,
            api_secret = "YOUR-API-SECRET"
        };

        var jsonVal = Json(jsonObject, JsonRequestBehavior.AllowGet);

        Uri address = new Uri("https://api.lanoba.com/authenticate");
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
        ServicePointManager.ServerCertificateValidationCallback = delegate
        {
            return
                true; //always trust the presented cerificate
        };
        request.Method = "post";
        request.ContentType = "text/json";
        string response = null;
        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            streamWriter.Write(jsonVal);
        }

        using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
        {
            var reader = new StreamReader(resp.GetResponseStream());
            response = reader.ReadToEnd();
        }
//I keep getting an error code back from the provider with no real error description
//so right now I am assuming that I am doing something wrong on my end

}

Any help would be appreciated. 任何帮助,将不胜感激。

EDIT: FINAL ANSWER: 编辑:最终答案:

After the help from Onkelborg, (thank you!), here is the working example: 在Onkelborg的帮助下(谢谢!),这是一个有效的示例:

   var wc = new WebClient();
    var wcResponse = wc.UploadValues("https://api.lanoba.com/authenticate", new System.Collections.Specialized.NameValueCollection() { { "token", Request["token"].ToString()}, { "api_secret", "Your-Secret-Api--" } });
    var decodedResponse = wc.Encoding.GetString(wcResponse);

Once again, thank you. 再次感谢您。

As far as I can tell you shouldn't be sending JSON at all.. :) 据我所知,您根本不应该发送JSON .. :)

Use the UploadStrings/UploadValues (don't remember the actual name.. :) ) on the WebClient class, it's pretty much exactly what you want - it will post a namevaluecollection to a given uri and return a string with the answer :) 在WebClient类上使用UploadStrings / UploadValues(不记得实际的名称。:)),这几乎正是您想要的-它会将namevaluecollection发布到给定的uri并返回带有答案的字符串:)

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

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