繁体   English   中英

C# .NET 框架 4.8 API 调用不适用于新的 URI

[英]C# .NET Framework 4.8 API Call not working with New URI

我在 C# (.NET Framework 4.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 在旧系统和新系统中都可以使用,只需将“Content-Type”从“application/xml”更改为“text/plain”。

        HttpURLConnection connection = null;
        URL newUrl = new URL(baseUrl);

        connection = (HttpURLConnection)newUrl.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        /** OLD SYSTEM TAKES "application/xml" FOR "Content-Type" */
        connection.setRequestProperty("Content-Type", "application/xml");
   
        /** NEW SYSTEM REQUIRES "text/plain" FOR "Content-Type" */
        connection.setRequestProperty("Content-Type", "text/plain"); 
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("POST");

- - 更新 - -

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

我的 API Class:

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 错误)我以这样一种方式调用它,这要归功于这段精彩的代码

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();
}

这是我得到的错误

{StatusCode:400,ReasonPhrase:'Bad Request',版本:1.1,内容:System.Net.Http.StreamContent,标题:{ X-Frame-Options:SAMEORIGIN X-XSS-Protection:1; mode=block X-Content-Type-Options: nosniff X-WebKit-CSP: default-src 'self'
X-Permitted-Cross-Domain-Policies:仅主连接:关闭
传输编码:分块缓存控制:无缓存 日期:格林威治标准时间 2021 年 1 月 27 日星期三 18:04:07 设置 Cookie:visid_incap_2284916=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; 到期=格林威治标准时间 2022 年 1 月 26 日星期三 20:08:03; 仅http; 路径=/; 域=.portal.conduent.com; 安全的; SameSite=None Set-Cookie: incap_ses_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 路径=/; 域=.My.Domain.com; 安全的; SameSite=None 服务器:Microsoft-IIS/10.0 X-Powered-By:ASP.NET
Strict-Transport-Security:max-age=31536000 X-CDN:Incapsula
X-Iinfo:3-9884384-9884391 NNNN CT(58 122 0) RT(12121212121 51) q(0 0 2 0) r(3 4) U6 内容类型:文本/纯文本; 字符集=utf-8 }}

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

暂无
暂无

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

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