Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#&x2B中调用外部API;具有凭据的WCF_C#_.net_Rest_Api_Wcf - Fatal编程技术网

如何在C#&x2B中调用外部API;具有凭据的WCF

如何在C#&x2B中调用外部API;具有凭据的WCF,c#,.net,rest,api,wcf,C#,.net,Rest,Api,Wcf,我正试图通过请求调用外部API,我有一个带有凭据的URL 无法从浏览器访问外部API string url = "https://ICSTESTQSic-tkmdlms.integration.ocp.oraclecloud.com:443/ic/api/integration/v1/flows/rest/TSA2ELOQUA_NURTURELEADS/1.0/NurturingLead"; using (var client = new WebClient())

我正试图通过请求调用外部API,我有一个带有凭据的URL

无法从浏览器访问外部API

string url = "https://ICSTESTQSic-tkmdlms.integration.ocp.oraclecloud.com:443/ic/api/integration/v1/flows/rest/TSA2ELOQUA_NURTURELEADS/1.0/NurturingLead";
        using (var client = new WebClient())
        {
            NetworkCredential credentials = new NetworkCredential("Username", "Password");
            client.Credentials = credentials;
            CredentialCache cc = new CredentialCache();
            cc.Add(
                new Uri(url),
                "Basic",
                new NetworkCredential("Username", "Password"));
            client.Credentials = cc;            

            using (Stream stream = client.OpenRead(url))
            using (StreamReader reader = new StreamReader(stream))
            {
                MessageBox.Show(reader.ReadToEnd());
            }
我想通过从我的c#代码发送一个对象作为请求来调用外部API。
API已通过身份验证。

如果使用webrequest或HttpClient,则可以编写以下代码将对象发送到服务器。(假设您关于身份验证的代码是正确的)

结果呢


URL不是API。添加单词REST只会给出API的模糊形状,指定允许的HTTP谓词。我使用了您的方法,但在获取流时出错。错误为:-“基础连接已关闭。发送时发生意外错误”。您可以参考下面的链接,它将post对象和向服务器发送证书相结合,这里它使用内容类型application/x-www-form-urlcoded发送post请求,如果您的内容类型是application json,请使用json谢谢您的回复。我能够发送请求,但在这行代码“HttpWebResponse=(HttpWebResponse)request.GetResponse()”上出现相同的错误“底层连接已关闭”。我正在使用.NET4.0。你能帮我解决这个问题吗。
WebRequest request = WebRequest.Create("http://localhost:60956/api/values");


        YClass yClass = new YClass() { Completion = 5, Message = "AA", Result = "Result" };
        JavaScriptSerializer serialize = new JavaScriptSerializer(); // the object is used to serialize the data , you could use any object that could serialize data
        byte[] objectContent = Encoding.UTF8.GetBytes(serialize.Serialize(yClass));
        request.ContentLength = objectContent.Length;
        request.ContentType = "application/json";
        request.Method = "POST";
        using (var stream = request.GetRequestStream())
        {
            stream.Write(objectContent, 0, objectContent.Length);
            stream.Close();
        }


        var resp = request.GetResponse();
        using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
        {
            string s = sr.ReadToEnd();
            System.Console.WriteLine(s);
        }