POST 201 CREATED c# 之后的轮询服务器

polling server after POST 201 CREATED c#

我可能犯了一个非常愚蠢的错误,我能够向服务器成功发出 POST 请求。我还能够从服务器 201 获得响应并能够查看 json("requestid")。我能够反序列化 JSON 并将 requestid 解析为字符串 (public string requestID)。我有一个计时器 (timer1) 设置为每 1 秒轮询一次服务器,如果 201 创建并执行,轮询应该成功启动。但是我遇到的问题是它不包括 requestid。有人能给我建议并告诉我哪里做错了吗?

namespace RestAPI
{
    public enum httpVerb
    {
        GET,
        POST,
        PUT,
        DELETE
    }


    class RESTAPI
    {
        public string endPoint { get; set; }
        public httpVerb httpMethod { get; set; }
        public string userPassword { get; set; }
        public int sendAmount { get; set; }
        public string location { get; set; }

        public string requestId { get; set; }



        public RESTAPI()
        {
            endPoint = string.Empty;
            httpMethod = httpVerb.GET;
            userPassword = string.Empty;
            //requestId = string.Empty; 

        }

        public string makeRequest()
        {
            string strResponseValue = string.Empty;
            string result = string.Empty;



            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);
            request.Method = httpMethod.ToString();
            request.ContentType = "application/json";
            request.Accept = "application/connect.v1+json";

            String username = "mokhan";
            String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + userPassword));
            request.Headers.Add("Authorization", "Basic " + encoded);

            if(httpMethod == httpVerb.POST)
            {
                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    string json = "{\"transactionType\":\"SALE\"," + "\"amount\":" + sendAmount + "," +
                                  "\"currency\":\"GBP\"}";

                    streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();
                }



                HttpWebResponse responseback = (HttpWebResponse)request.GetResponse();
                //string result;
                using (StreamReader rdr = new StreamReader(responseback.GetResponseStream()))
                {

                    result = rdr.ReadToEnd();
                }


                if (responseback.StatusCode == HttpStatusCode.Created)
                {
                    dynamic jsonObj = JsonConvert.DeserializeObject(result);
                    requestId = jsonObj.requestId.ToString();
                    return requestId;
                }
                return result;

            }

            HttpWebResponse response = null;

            try
            {                
                response = (HttpWebResponse)request.GetResponse();
                using (Stream responseStream = response.GetResponseStream())
                    {
                       if (responseStream != null)
                          {
                            using (StreamReader reader = new StreamReader(responseStream))
                            {
                                strResponseValue = reader.ReadToEnd();
                            }
                        }   
                    }                 
                }


            catch (Exception ex)
            {

            }

            finally
                {
                    if (response != null)
                    {
                        ((IDisposable)response).Dispose();
                    }
                }

                return strResponseValue;
            }

    }
}

下面的代码显示了我对服务器的 POST 和 GET 请求,我在收到响应后在我的 POST 请求中添加了计时器方法并添加了用于轮询的代码我的计时器方法。我还设置了字符串 transactionid = rclient.requestId; 并在 rclient.endPoint = "https://" + txtBox.Text + "/pac" + "/terminals/" + txtBox3.Text + "/transactions/" + transactionid; 上调用每 1 秒轮询一次服务器,但由于某种原因它没有接收到 transactionid

namespace RestAPI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void go_Click(object sender, EventArgs e)
        {
            RESTAPI rclient = new RESTAPI();
            rclient.endPoint = "https://" + txtBox.Text + "/pac" + "/terminals/" + txtBox3.Text;
            rclient.userPassword = txtbox2.Text;
            debugOutput("REQUEST SENT");
            string strResponse = string.Empty;
            strResponse = rclient.makeRequest();

            debugOutput(strResponse);
        }

        private void debugOutput(string strDebugText)
        {
            try
            {
                System.Diagnostics.Debug.Write(strDebugText + Environment.NewLine);
                txtBoxResponse.Text = txtBoxResponse.Text + strDebugText + Environment.NewLine;
                txtBoxResponse.SelectionStart = txtBoxResponse.TextLength;
                txtBoxResponse.ScrollToCaret();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write(ex.Message, ToString() + Environment.NewLine);

            }
        }

        private void txtBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void Test_Click(object sender, EventArgs e)
        {
            RESTAPI rclient = new RESTAPI();
            rclient.httpMethod = httpVerb.POST;
            rclient.sendAmount = Convert.ToInt32(amount.Text);
            rclient.endPoint = "https://" + txtBox.Text + "/pac" + "/terminals/" + txtBox3.Text + "/transactions";
            rclient.userPassword = txtbox2.Text;

            debugOutput("REQUEST SENT");

            string strResponse = string.Empty;
            strResponse = rclient.makeRequest();
            debugOutput(strResponse);


            timer1.Start();


    }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            RESTAPI rclient = new RESTAPI();
            rclient.httpMethod = httpVerb.GET;
            string transactionid = rclient.requestId;
            rclient.endPoint = "https://" + txtBox.Text + "/pac" + "/terminals/" + txtBox3.Text + "/transactions/" + transactionid; 
            debugOutput("REQUEST SENT");
            string strResponse = string.Empty;
            strResponse = rclient.makeRequest();
            debugOutput(strResponse);
        }
    }
}

刚刚解决了这个问题,在我的 public class RESTAPI 中,我取消了评论 //requestId = string.Empty; 完成后,我能够将请求 ID 作为字符串获取,然后在我的计时器中调用它 字符串 transactionid = rclient.requestId;