web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

POST text file in AX2012 using HttpWebRequest

(0) ShareShare
ReportReport
Posted on by 800

Hi All,

when i am  trying to post text file by using below code(through web request) and every thing looks fine and am getting success response. But file is not getting uploaded at destination. Can some one suggest me on this. 

I have converted files in to bytes in different but no luck.

************************************************************************

private void Upload()
{
System.Net.HttpWebRequest webReq;
System.Net.HttpWebResponse webRes;
System.Exception ex,webexception;

CLRObject clrObj,httpresponse;
System.IO.Stream stream;
InteropPermission permission;
System.IO.StreamReader streamRead;
System.IO.StreamWriter streamWrite;
System.Net.ServicePoint servicePt;
System.Text.Encoding utf8;
System.String filecontents;
System.IO.StreamReader reader;
System.Byte[] bytes,byteArray;
System.Net.ICredentials credentialCache;
System.Net.NetworkCredential NetworkCredential = new System.Net.NetworkCredential();
System.Net.CredentialCache myCredCache = new System.Net.CredentialCache();
System.Uri URL = new System.Uri('http://file/upload');

FilePath file;
str byteStr,str64;
Bindata bindata;
int bn;

System.Net.WebHeaderCollection headers = new System.Net.WebHeaderCollection();

NetworkCredential = System.Net.CredentialCache::get_DefaultNetworkCredentials();
myCredCache.Add(URL,"NTLM",NetworkCredential);

file = @"D:\test\B0000001.txt";


bindata = new bindata();
bindata.loadFile(file);
str64 = bindata.base64Encode();
utf8 = System.Text.Encoding::get_UTF8();
bytes = utf8.GetBytes(str64);

//byteArray = utf8.GetBytes(byteStr);
//byteStr = System.Convert::ToBase64String(byteArray);
//
//This line gives the user control to run unmanaged and managed code
new InteropPermission(InteropKind::ClrInterop).assert();

// Create Object for adding headers

clrObj = System.Net.WebRequest::Create(URL);
webReq = clrObj;
webReq.set_Credentials(myCredCache);
webReq.set_PreAuthenticate(true);
webReq.set_UnsafeAuthenticatedConnectionSharing(true);
webReq.set_AllowReadStreamBuffering(false);
webReq.set_AllowWriteStreamBuffering(false);

//Set Method Type
webReq.set_Method('POST');
webReq.set_KeepAlive(true);
webReq.set_Timeout(10000);

// Set Content type
webReq.set_ContentType('multipart/form-data;boundary=boundary');
webReq.set_Accept('multipart/form-data;boundary=boundary');
webReq.set_ContentLength(bytes.get_Length());

//Get Service Point
servicePt = webReq.get_ServicePoint();
servicePt.set_Expect100Continue(true);

//Gets the response object
try
{
Stream = webreq.GetRequestStream();
Stream.Write(bytes,0,bytes.get_Length());
Stream.Flush();
Stream.Close();
Stream.Dispose();
webres = webreq.GetResponse();
info(webres.get_StatusDescription());
webRes.Close();
}
catch (Exception::CLRError)
{
ex = CLRInterop::getLastException();
info(ex.ToString());
if (ex != null)
{
ex = ex.get_InnerException();
if ((ex != null) && (ex is System.Net.WebException))
{
webException = ex as System.Net.WebException;
info(webexception.ToString());
}
}

}

}

**********************************************************************

Note :-  I have verified and bytes is not zero. it has data.

I have to use NTLM only. it's mandate at our end.

Regards,

KVNKK

I have the same question (0)
  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    What kind of service endpoint is it? Are you able to debug the service?

    By the way, I see an unrelated flaw in your code. You dispose resources only if everything succeed but not in case of failure. Also, Stream.Close() and Stream.Dispose() do the same thing (Close() calls Dispose()), therefore using just one of them is sufficient.

  • kvnkk Profile Picture
    800 on at

    1. This server endpoint will accepts the payment file in form of .txt  and will upload in to bank SFTP server.

    2. I don't have option to  debug the code, only  can debug AX job.

    3. will do necessary change regarding close() and Dispose(). thanks

    Note :- I can't call bank SFTP server directly due to some restriction.

  • Verified answer
    Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    If the service gets called, it says that everything is correct but it doesn't do its job, there is a problem inside the service. It should either do its stuff or return an error. If you have no means of debugging it (such as logging; it doesn't have to mean walking through source code in a debugger), then you'll have to turn to the vendor of the service

    You didn't provide any details, so it's very difficult to comment on the implementation. The content headers suggest that you're probably simulating an HTML form. Is that correct?

  • Suggested answer
    Fangyuan Hou Profile Picture
    on at

    Hi kvnkk,

    Could you please have a check the below thread? there is one example of NTLM authentication, it's said set the HttpWebRequest.PreAuthenticate property to false. Would you please have a try?

    https://support.microsoft.com/en-ph/help/908573/a-post-or-put-request-may-fail-when-you-use-the-httpwebrequest-class-t

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 660 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 549 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 307 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans