该流不支持并发IO读取或写入操作

时间:2018-04-02 16:25:46

标签: c# console-application

我试图在Stack Overflow中搜索此问题,但无法找到正确的答案,因此在此处发布我的问题。下面是我的代码,基本上我试图从文件中读取内容并将其发布到web api,然后用另一个文件重复相同的步骤。第一个调用通过但第二个调用失败并显示错误:

The stream does not support concurrent IO read or write operations at this line requestStream.Write(buffer, 0, bytesRead);

请告诉我,我在这里做错了什么?

using(FileStream fs = new FileStream(@ "C:\Test1.txt", FileMode.Open, FileAccess.Read)) {

 byte[] buffer = null;
 int bytesRead = 0;
 using(Stream requestStream = request.GetRequestStream()) {
  buffer = new Byte[checked((uint) Math.Min(1024, (int) fs.Length))];
  while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0) {
   requestStream.Write(buffer, 0, bytesRead);
  }
  requestStream.Flush();
 }
}
using(FileStream fs = new FileStream(@ "C:\Test2.txt", FileMode.Open, FileAccess.Read)) {

 byte[] buffer = null;
 int bytesRead = 0;
 using(Stream requestStream = request.GetRequestStream()) {
  buffer = new Byte[checked((uint) Math.Min(1024, (int) fs.Length))];
  while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0) {
   requestStream.Write(buffer, 0, bytesRead);
  }
  requestStream.Flush();
 }
}

1 个答案:

答案 0 :(得分:2)

我认为问题正在发生,因为您正在尝试获取不再可用的请求流:

试试这段代码:

        List<string> files = new List<String>();
        files.Add(@"C:\Test1.txt");
        files.Add(@"C:\Test2.txt");
        using (Stream requestStream = request.GetRequestStream())
        {
            files.ForEach(fileName =>
            {
                byte[] buffer = null;
                int bytesRead = 0;
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    buffer = new Byte[checked((uint)Math.Min(1024, (int)fs.Length))];
                    while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        requestStream.Write(buffer, 0, bytesRead);
                    }
                    requestStream.Flush();
                }
            });
        }

更新:要控制端点和文件,请尝试以下操作:

    static void Main(string[] args)
    {
        string result = SendPost(@"C:\Test1.txt", "https://httpbin.org/post");
        if(result.Contains("SUCCESS"))
            SendPost(@"C:\Test2.txt", "https://httpbin.org/anotherpost");

    }

    static string SendPost(string filename, string URL)
    {

        var httpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
        httpWebRequest.ContentType = "text/plain";
        httpWebRequest.Method = "POST";

        /*proxy config*/
        WebProxy proxy = new WebProxy();
        Uri newUri = new Uri("http://xxxxxx");
        proxy.Address = newUri;
        httpWebRequest.Proxy = proxy;

        using (var sw = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string[] lines = File.ReadAllLines(filename);
            for(int i=0; i<lines.Length; i++)
                sw.WriteLine(lines[i]);
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            return result;
        }
    }