Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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#.NET Framework 4.8 API调用无法使用新URI_C#_Api_Post_.net Framework 4.8 - Fatal编程技术网

C#.NET Framework 4.8 API调用无法使用新URI

C#.NET Framework 4.8 API调用无法使用新URI,c#,api,post,.net-framework-4.8,C#,Api,Post,.net Framework 4.8,我在C#(.NETFramework4.8)中有一篇API文章正在运行。已使用新URI创建了一个新API。(我无法访问API“黑盒”),当我将现有代码指向新的API URI时,它不起作用。我得到了以下指导: Use a "Content Type" of "text/plain" (the old used "application/xml") Add the parameters to the body of the call.

我在C#(.NETFramework4.8)中有一篇API文章正在运行。已使用新URI创建了一个新API。(我无法访问API“黑盒”),当我将现有代码指向新的API URI时,它不起作用。我得到了以下指导:

 Use a "Content Type" of "text/plain" (the old used "application/xml")
 Add the parameters to the body of the call. (I had this in a querystring in the old version.)
以下是在当前API中工作的c代码:

  public static string HttpPost(string URI, string Parameters)
    {
        try
        {
            //URI = System.Configuration.ConfigurationManager.AppSettings["SiteURL"];
            System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
            //req.Proxy = new System.Net.WebProxy(ProxyString, true);
            //Add these, as we're doing a POST
            req.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            req.Method = "POST";
            //We need to count how many bytes we're sending. Post'ed Faked Forms should be name=value&
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
            req.ContentLength = bytes.Length;
            System.IO.Stream os = req.GetRequestStream();
            os.Write(bytes, 0, bytes.Length); //Push it out there
            os.Close();
            System.Net.WebResponse resp = req.GetResponse();
            if (resp == null) return null;
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            string postXML = sr.ReadToEnd().Trim();
            resp.Close();
            return postXML;
        }
        catch(Exception ex)
        {
            return "could not pull" + ex.Message;
        }
        
    }
以下是既适用于旧系统,也适用于新系统的原始Java,只需将“内容类型”从“应用程序/xml”更改为“文本/普通”。:

----更新----

我将内容序列化为JSON,但仍然得到一个400错误。我正在传递3个参数。。。主体中的encq、KS和DS。我的理解是,如果你有一个复杂的对象(json),它会自动进入身体,这就是他们说它必须在的地方。我几乎没有其他指导,因为这是C#中的第三方API,我没有相关信息。重申一下,他们正在从一个系统转移到另一个系统,他们试图“让它变得简单”,所以我所要做的就是更改我的URI。这对我来说一点也不容易。你的结果(希望)会有所不同。。。感谢所有的帮助,并提前感谢新的方向/指导:

我的API类:

public class APIParameters
{
    public string ENCQS { get; set; }
    public string KS { get; set; }
    public string DS { get; set; }
    public APIParameters()
    {
        ENCQS = "";
        KS = "";
        DS = "";
    }
}
public class APICall
{ 
    public string URI { get; set; }
    public string ContextType { get; set; } = "application/xml";
    public string Response { get; set; }
    public APIParameters Params = new APIParameters();
    
    public APICall()
    {
        URI = "";
        ContextType = "application/xml";
        Response = "";

    }

}
我的新HTTP API调用(出现400错误)我调用此函数的方式使其同步,这要感谢:

这是我得到的错误

{状态代码:400,原因短语:“错误请求”,版本:1.1,内容: System.Net.Http.StreamContent,标题:{X-Frame-Options: SAMEORIGIN X-XSS-Protection:1;模式=块X-Content-Type-Options: nosniff X-WebKit-CSP:默认src“self”
X-Allowed-Cross-Domain-Policys:仅主控连接:关闭
传输编码:分块缓存控制:无缓存日期:周三,27 2021年1月18:04:07格林尼治标准时间集Cookie: visid_incap_2284916=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; expires=Wed,2022年1月26日20:08:03 GMT;HttpOnly;path=/; Domain=.portal.conduent.com;安全;SameSite=None Set Cookie: 是啊,是啊,是啊,是啊,是啊; 路径=/;域=.My.Domain.com;安全;SameSite=None服务器: Microsoft IIS/10.0 X-Powered-By:ASP.NET
严格的运输安全:最大年龄=31536000 X-CDN:Incapsula
X-Iinfo:3-9884384-9884391北马里亚纳CT(58 122 0)RT(1212121 51)q(0 0 2 0)r(3 4)U6内容类型:text/plain;charset=utf-8}


有时,它就像在命令行中刷新DNS缓存一样简单:
ipconfig/flushdns
-对于MS Windows。

您是否考虑过
HttpClient
它将为您节省大量样板文件您是否可以至少发布异常消息,不要返回
StackTrace
,这在这种情况下基本上是不相关的,至少返回
ex.Message
,或
ex.ToString()
,以获取外部和内部异常消息。您原来的异常处理比现在更改的更有用。与几乎所有“我无法让此api工作”问题一样,请使用postman,让它在那里工作,当它工作时,创建C#代码,当C#代码不起作用时,使用fiddler检查两个请求,然后根据这些信息创建一个问题,显示调用中的差异、导致问题的最小C#代码(理想情况下是HttpClient)以及原始请求。。。百分之九十八的情况下,你会有足够的信息自己解决问题。我试过邮递员和小提琴手,也会遇到同样的400个错误。(我将把这一点包括在我原来的问题中)我也试过了。还是不走运。
public class APIParameters
{
    public string ENCQS { get; set; }
    public string KS { get; set; }
    public string DS { get; set; }
    public APIParameters()
    {
        ENCQS = "";
        KS = "";
        DS = "";
    }
}
public class APICall
{ 
    public string URI { get; set; }
    public string ContextType { get; set; } = "application/xml";
    public string Response { get; set; }
    public APIParameters Params = new APIParameters();
    
    public APICall()
    {
        URI = "";
        ContextType = "application/xml";
        Response = "";

    }

}
HttpResponseMessage response;
string responseMsg = "";

System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
using (var client = new HttpClient())
{
    var content = new StringContent(JsonConvert.SerializeObject(API.Params), Encoding.UTF8, "application/json");
    content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/plain");
    response = await client.PostAsync(URI, content);
    responseMsg = await response.Content.ReadAsStringAsync();
}