[AX2012中使用HttpWebRequest的POST XML文件

问题描述 投票:1回答:1

我需要调用一个API,该API接受以单个XML文件为参数的POST请求。我尝试过通过邮递员(具有表单数据内容类型)调用资源,并且似乎可以正常工作。尝试在AX中实施此操作时,服务器出现错误500。

这是POST请求:

void executeRequest(XmlDocument _data)
{

    clrRequestObject = System.Net.WebRequest::Create("/URL/WEB/API/");
    webRequest = clrRequestObject;

    //Set Method Type
    webRequest.set_Method("POST");
    webRequest.set_CookieContainer(cookieCon);
    webRequest.set_KeepAlive(true);

    new InteropPermission(InteropKind::ClrInterop).assert();

    // Set Content type
    webRequest.set_ContentType("multipart/form-data; boundary=---------------------------7db2511d20514");
    byteArray = this.getMultipartFormData(_data,"filename");

    inputStream = webRequest.GetRequestStream();
    inputStream.Write(byteArray, 0, byteArray.get_Length());

    webResponse = webRequest.GetResponse();

    inputStream.Close();
    stream.Close();
    webResponse.Close();

}


private System.Byte[] getMultipartFormData(XmlDocument _data, str name)
{
    #define.DocFieldFormat('--\%1\r\nContent-Disposition: form-data; name=\"\%2\"; filename=\"\%3\";\r\nContent-Type: ' + '\%4\r\n\r\n')
    #define.Dot('.')

    System.IO.MemoryStream memstream = new System.IO.MemoryStream();
    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
    BinData binData = new BinData();
    System.Byte[] bytes;
    str s;
    XMLWriter xmlWriter;
    str path = WinAPI::getTempPath();
    Filename filename = path+name+".xml";
    System.IO.FileStream fileStream;

    xmlWriter = XmlWriter::newFile(filename);
    _data.writeTo(XmlWriter);
    xmlWriter.close();

        // Header
        s = strFmt(
            #DocFieldFormat,
            ---------------------------7db2511d20514,
            name,
            name+#Dot+"xml",
            "text/xml");
        bytes = encoding.GetBytes(s);
        memstream.Write(bytes, 0, bytes.get_Length());

        /*Alternative attempt to set content 1*/
        //binData.setStrData(_data.outerXml());
        //bytes = encoding.GetBytes(binData.base64Encode());
        //memstream.Write(bytes, 0, bytes.get_Length());

        /*Alternative attempt to set content 2*/
        //bytes = encoding.GetBytes(_data.outerXml());
        //memstream.Write(bytes, 0, bytes.get_Length());

        /*Alternative attempt to set content 3*/
        fileStream = System.IO.File::OpenRead(filename);
        fileStream.CopyTo(memstream);

    }

    s = "\r\n--" + #FormDataBoundary + "--\r\n";
    bytes = encoding.GetBytes(s);
    memstream.Write(bytes, 0, bytes.get_Length());

    bytes = memstream.ToArray();
    memstream.Close();

    return bytes;
}

我正在调查API服务器上的确切错误是为了帮助我进行故障排除,但是正在寻找有关我可能做错了什么的建议。是否有通过AX执行此操作的替代方法?

c# .net axapta dynamics-ax-2012 dynamics-ax-2012-r3
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.