我在StreamUtil.Copy遇到失败问题而错误为"请求已中止且请求被取消" 。我已经尝试通过增加请求的超时atrribute,尽管它失败了。任何建议都会有所帮助。我已经尝试过在内存流中传递请求,但没有运气。
string MIMEBoundary = "--MIMEBoundary633737556018130000";
Uri urlUri = new Uri(url);
Logger.Debug("Preparing form post...");
using (MemoryStream body = new MemoryStream())
{
this.GetPostData(body, MIMEBoundary, paramList);
body.Position = 0;
HttpWebRequest myWeb = (HttpWebRequest)WebRequest.Create(urlUri);
//ServicePointManager.DefaultConnectionLimit = 100;
ServicePointManager.MaxServicePointIdleTime = 10000;
System.Net.ServicePointManager.Expect100Continue = false;
myWeb.AllowAutoRedirect = true;
myWeb.KeepAlive = false;
if (ignoreCertErrors)
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
if (cookieContainer.Count > 0) {
Logger.Debug("Cookie container got cookies, len: " + cookieContainer.Count);
myWeb.CookieContainer = cookieContainer;
}
if (proxyHost != null)
{
Logger.Debug("Using proxyHost: " + proxyHost);
myWeb.Proxy = new WebProxy(proxyHost, proxyPort.Value);
}
// force no proxy for private addrs
Uri uri = new Uri(url);
if (uri.Host.StartsWith("10.") || uri.Host.StartsWith("1.1.sample ip"))
myWeb.Proxy = new WebProxy();
// myWeb.Method = "PS";
myWeb.Method = type;
myWeb.AllowWriteStreamBuffering = true;
// myWeb.AllowWriteStreamBuffering = false;
myWeb.ProtocolVersion = HttpVersion.Version10;
myWeb.Timeout = timeoutMS;
// myWeb.Timeout =-1;// System.Threading.Timeout.Infinite;
if (credential != null)
myWeb.Credentials = credential;
string auxContent = "multipart/form-data;";
auxContent += "boundary=" + MIMEBoundary;
myWeb.ContentType = auxContent;
myWeb.Accept = "iso-8859-1";
myWeb.ContentLength = body.Length;
// myWeb.ContentLength =byteArray.Length;
Stream reqStream = myWeb.GetRequestStream();
Parts.IO.StreamUtil.Copy(body, reqStream);
//try
//{
// body.Seek(0, SeekOrigin.Begin);
// body.CopyTo(myWeb.GetRequestStream());
//}
//catch (Exception exp)
//{
// Logger.Error("Publishing to Appstudio failed.", exp);
//}
// myWeb.GetRequestStream().CopyTo(body);
// body.Write(
//using (var ms = new MemoryStream())
//{
// // Copy the response stream to the memory stream.
// myWeb.GetRequestStream().CopyTo(ms);
// ms.Write(myWeb, 0, myWeb.Length);
//}
//myWeb.GetRequestStream().CopyTo();
/* string json_value = jsonSerializer.Serialize(data); //Body data
ServicePointManager.Expect100Continue = false;
using (var streamWriter = new StreamWriter(myWeb.GetRequestStream()))
{
streamWriter.Write(json_value);
streamWriter.Flush();
streamWriter.Close();
}
Stream responseStream = myWeb.GetResponse();
StreamReader myStreamReader = new StreamReader(responseStream);
responseData = myStreamReader.ReadToEnd();
Parts.IO.StreamUtil.Copy(body, ms);*/
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myWeb.GetResponse();
return myHttpWebResponse;
堆栈跟踪如下
at System.Net.ConnectStream.InternalWrite(Boolean async,Byte [] buffer,Int32 offset,Int32 size,AsyncCallback callback,Object state) 在System.Net.ConnectStream.Write(Byte []缓冲区,Int32偏移量,Int32大小) at Parts.IO.StreamUtil.Copy(流输入,流输出)在C:\ dev \ parts \ DotnetParts \ IO \ StreamUitl.cs