小编Mac*_*ume的帖子

在循环中使用StreamWriter时出现“ Stream not Writable”错误

我试图用C#编写工具来帮助QA一些网络问题,并且遇到了问题。该程序应该每秒发送JSON格式的查询到服务器。

目前,它只能运行一次,但是在第二次尝试发送查询时,我遇到了一个异常,因为

“流不可写。”

这是我的代码:

public partial class Form1 : Form
{
    Timer timer1;
    String query;
    String result;

    HttpWebRequest request;
    StreamWriter writeData;
    StreamReader readData;
    HttpWebResponse response;


    public Form1()
    {
        InitializeComponent();
        timer1 = new Timer();
        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Interval = 1000;
        File.Delete(AppDomain.CurrentDomain.BaseDirectory + "log.txt");
        logOutput.ReadOnly = true;
        request = (HttpWebRequest)WebRequest.Create("a URL goes here");
        request.ContentType = "application/json";
        request.Method = "POST";
        query = "{some json stuff goes here}";
    }

    private void startButton_Click(object sender, EventArgs e)
    {
        if (!timer1.Enabled)
        {
            timer1.Start();
            startButton.Text = "Stop"; …
Run Code Online (Sandbox Code Playgroud)

c# json streamwriter

5
推荐指数
1
解决办法
5213
查看次数

标签 统计

c# ×1

json ×1

streamwriter ×1