Hi,
We are getting below error while uploading file to sharepoint document Library Using HttpWebRequest PUT Method in asp.net.
Error Message : The underlying connection was closed: The connection was closed unexpectedly.
below is my code to upload file to sharepoint document Library Using HttpWebRequest PUT Method.
Please Suggest.
string dateFormat = DateTime.Now.ToString("MMddyy");
string FileName = OutputFileName + dateFormat + ".xlsx";
string uploadedFilePath = Path.Combine(OutputPath, FileName);
string sharePointListPath = SharePointSiteURL;
HttpWebResponse SPOResponse = null;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(sharePointListPath + FileName);
try
{
request.ProtocolVersion =
HttpVersion.Version10;
request.KeepAlive =
false;
request.Timeout = 1000000000;
request.ReadWriteTimeout = 1000000000;
request.Proxy =
WebRequest.GetSystemWebProxy();// WebRequest.DefaultWebProxy;
System.Net.
ServicePointManager.Expect100Continue = false;
NetworkCredential nc = new NetworkCredential(wsgSettings.ServiceAccountUserID, wsgSettings.ServiceAccountPassword);
request.Credentials = nc;
request.Method =
"PUT";
byte[] buffer = new byte[4096];
using (Stream stream = request.GetRequestStream())
{
using (FileStream fsWorkbook = File.Open(uploadedFilePath,
FileMode.Open, FileAccess.Read))
{
int i = fsWorkbook.Read(buffer, 0, buffer.Length);
while (i > 0)
{
stream.Write(buffer, 0, i);
i = fsWorkbook.Read(buffer, 0, buffer.Length);
}
}
}
SPOResponse = (
HttpWebResponse)request.GetResponse();
SPOResponse.Close();
}
catch (Exception ex)
{
}
finally
{
request.Abort();
}