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