我正在尝试使用带有凭据的URL的请求调用外部API。
无法从浏览器访问外部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已通过身份验证。
答案 0 :(得分:0)
如果使用webrequest或HttpClient,则可以编写以下命令将对象发送到服务器。(假设您的身份验证代码正确)
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);
}
结果。