我可能犯了一个非常愚蠢的错误,我能够向服务器发出成功的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请求中添加了timer方法以启动,并在timer方法中添加了用于轮询的代码。我还设置了字符串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);
}
}
}
答案 0 :(得分:0)
刚刚解决了这个问题,在我的公共类RESTAPI中,我对此未发表评论 // requestId = string.Empty; 完成后,我可以将请求ID提取为字符串,然后在计时器中调用它 字符串transactionid = rclient.requestId;