你好,我正在尝试使用Web服务从一个文件夹发送XML文件到本地计算机的另一个文件夹, 文件已成功发送并且应用程序已正常关闭,但问题是文件在目标文件夹中需要很长时间才能修改,当我再次尝试调用Web服务时,它会给我超时异常。 以下是我的代码:
public void SendC_XML(string path, string queueID, string application, string servicewTMURI)
{
HttpWebRequest httpWebRequest2 = null;
//Stream memStream = null;
//FileStream fileStream = null;
String[] listOfFiles = Directory.GetFiles(path, "*.xml");
for (int i = 0; i < listOfFiles.Length; i++)
{
try
{
MyLog.Add("found path " + listOfFiles[i]);
string[] ImagesArray = listOfFiles[i].Split('\\');
string ImageName = ImagesArray[ImagesArray.Length - 1];
int index = ImageName.IndexOf(".");
if (index > 0)
ImageName = ImageName.Substring(0, index);
MyLog.Add("image name = " + ImageName);
string[] textArray1 = new string[] { servicewTMURI, "/Queue/SetFile/", application, "/", queueID, "/" + ImageName + "/xml" };
string requestUriString = string.Concat(textArray1);
Guid guid = new Guid();
string boundary = "---------------------------" + guid;
httpWebRequest2 = (HttpWebRequest)WebRequest.Create(requestUriString);
httpWebRequest2.ContentType = "multipart/form-data; boundary=" +
boundary;
MyLog.Add("ContentType = " + httpWebRequest2.ContentType);
httpWebRequest2.Method = "POST";
httpWebRequest2.KeepAlive = false;
httpWebRequest2.Timeout = 5000;
httpWebRequest2.Proxy = null;
httpWebRequest2.ServicePoint.ConnectionLeaseTimeout = 5000;
httpWebRequest2.ServicePoint.MaxIdleTime = 5000;
httpWebRequest2.Credentials =
System.Net.CredentialCache.DefaultCredentials;
using (Stream memStream = new System.IO.MemoryStream())
{
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
boundary + "\r\n");
string formdataTemplate = "\r\n--" + boundary +
"\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
memStream.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";
//for (int i = 0; i < files.Length; i++)
//{
//MyLog.Add("file = " + files[i]);
string header = string.Format(headerTemplate, "uplTheFile", listOfFiles[i]);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
using (FileStream fileStream = new FileStream(listOfFiles[i], FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
memStream.Write(boundarybytes, 0, boundarybytes.Length);
fileStream.Close();
}
httpWebRequest2.ContentLength = memStream.Length;
using (Stream requestStream = httpWebRequest2.GetRequestStream())
{
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
}
//httpWebRequest2.Abort();
httpWebRequest2 = null;
//webResponse2 = null;
}
}
catch (Exception ex)
{
MyLog.Add("error in cXML method = " + ex.Message);
}
finally
{
//memStream.Close();
//fileStream.Close();
//requestStream.Close();
try
{
if (httpWebRequest2 != null)
{
MyLog.Add("heeeeeeeeeeeeeeey request != null");
httpWebRequest2.GetRequestStream().Close();
httpWebRequest2 = null;
}
}
catch (Exception exception2)
{
throw exception2;
}
}
}
}