Base solution for your next web application
Open Closed

Cannot access a third party API from within ASP.NET Zero but can from console app #5947


User avatar
0
dparizek created

The following code works great in a .NET framework 4.6-4.6.2 console app, but will not work when I bring it into the Angular1/MVC version of ASP.NET Zero...

      `  string xmldata = "<records><item><record>";
        xmldata += ptid;
        xmldata += "</record><field_name>";
        xmldata += fieldname;
        xmldata += "</field_name><value>" + newvalue + "</value><redcap_event_name>";
        xmldata += redcap_event_name;
        xmldata += "</redcap_event_name></item></records>";

        var sbPostData = new StringBuilder();
        sbPostData.Append("token=" + token);
        sbPostData.Append("&content=record");
        sbPostData.Append("&format=xml");
        sbPostData.Append("&type=eav");
        sbPostData.Append("&overwriteBehavior=normal");
        sbPostData.Append("&data=" + xmldata);
        sbPostData.Append("&returnContent=ids");
        sbPostData.Append("&returnFormat=xml");

        var byteArray = Encoding.UTF8.GetBytes(sbPostData.ToString());

        string Baseurl = "https://redcap.uahs.arizona.edu/api/";
        Uri REDCapUri = new Uri(Baseurl);

        var webRequest = WebRequest.CreateHttp(REDCapUri);
        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";

        try
        {
            webRequest.GetRequestStream().WriteAsync(byteArray, 0, byteArray.Length);
            var webResponse = webRequest.GetResponse();
            return "success";
        }
        catch (Exception e)
        {
            Debug.WriteLine(String.Concat(e.Message, e.StackTrace));
            return e.Message;
        }`
        

At this line: var webResponse = webRequest.GetResponse();

I get this error returned:

Exception thrown: 'System.Net.WebException' in System.dll The underlying connection was closed: An unexpected error occurred on a send. at System.Net.HttpWebRequest.GetResponse()

Why might it work in the console app but not within ASP.NET Zero framework? Any hints what I might try?


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @dparizek,

    Have you tried adding XmlMediaTypeFormatter to formatters ? You can read teh details here https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/json-and-xml-serialization#xml-media-type-formatter.

    As far as I remember, there was a problem when using XML with ABP generated api services. So, I suggest you to use a raw WEB API controller for this.

  • User Avatar
    0
    dparizek created

    Doesn't that apply more if I am trying to create the server side of the API - the endpoints?

    Above I am trying to consume a third party API... someone's else data api on the web from my app... not trying to be on the producer side.

    So I just need to send them the xml in plain text... I don't need it formatted... in postman or a console app the above just works to access the third party app... so what could be making that code not work if I put it in ABP?

    I search on the error text and I get references to SSL / TSL issues but I have verified that does not seem to be the issue in my case.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @dparizek

    Sorry, I got it wrong. Could you share the working request from Postman ?