我有以下代码将数据发布到远程Web服务。
问题在于,有时在Web服务上会多次记录事务。以下是代码:
public static T HttpPost<T>(string device_id, string outURL, string Accept, string ContentType, string XAuthToken, string JSONData, dynamic IbObject) {
T response = (T)Activator.CreateInstance(typeof(T));
HttpWebRequest Request = null; //Declare an HTTP-specific implementation of the WebRequest class.
HttpWebResponse Response = null;
LogTransaction(outURL, IbObject, HttpIO.output);
Global.DebugLog.LogMessage("HttpPost", JsonConvert.SerializeObject(IbObject), ErrorLevel.Info);
byte[] data2 = Encoding.ASCII.GetBytes(JSONData);
try
{
Request = (HttpWebRequest)HttpWebRequest.Create(outURL);
Request.Method = "POST";
Request.Headers.Add("X-OP-TOKEN", LocalACPInformation.urltoken);
Request.Headers.Add("X-DEVICE-ID", device_id);
Request.Accept = Accept;
Request.ContentType = ("application/json; charset=utf-8");
Request.ContentLength = data2.Length;
Request.Timeout = (1000000);
Stream myStream = Request.GetRequestStream();
myStream.Write(data2, 0, data2.Length);
myStream.Close();
Console.WriteLine("Message sent getting response... \n");
Response = (HttpWebResponse)Request.GetResponse();
Console.WriteLine(Response.Headers.ToString() + "\n");
System.IO.StreamReader sr = new System.IO.StreamReader(Response.GetResponseStream());
string rcv_data = sr.ReadToEnd().Trim();
IbObject = JsonConvert.DeserializeObject<T>(rcv_data);
Request.Abort();
return IberonObject;
}