You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

116 lines
3.9 KiB

using Newtonsoft.Json;
using OT.COM.SignalerMessage;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
namespace JeepConsole
{
public class CReqestItem
{
public CRequestMessage Req { get; set; }
}
public class CReqestPack
{
public List<CReqestItem> Reqs { get; set; }
public string Token { get; set; }
}
public class CResponsePack
{
public List<CResponseMessage> Reps { get; set; }
}
public class SendWebApiRequest
{
public string RunEncryptRequest(string i_sURL, CReqestPack i_crm, out string o_sRes)
{
string sData = JsonConvert.SerializeObject(i_crm);
return runRequest(i_sURL, sData, out o_sRes);
}
protected string runRequest(string i_sURL, string i_sContent, out string o_sRes,
[System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
[System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
[System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
{
string sRes = null;
Stream dataStream = null;
WebResponse response = null;
StreamReader reader = null;
string sMsg = null;
string responseFromServer = null;
try
{
// https://msdn.microsoft.com/zh-tw/library/debx8sh9(v=VS.110).aspx
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create(i_sURL);
request.Timeout = 600000;
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
request.UseDefaultCredentials = true;
string postData = i_sContent;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/json";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
response = request.GetResponse();
// Display the status.
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
reader = new StreamReader(dataStream);
// Read the content.
responseFromServer = reader.ReadToEnd();
// Display the content.
sRes = responseFromServer;
}
catch (Exception ex)
{
sMsg = $"{nameof(runRequest)} unknwon exception. i_sURL={i_sURL}, i_sContent={i_sContent}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
sMsg += ex.StackTrace;
sMsg += " " + responseFromServer;
#if DEBUG
System.Diagnostics.Debug.WriteLine(sMsg);
#endif
}
finally
{
if (null != reader)
{
reader.Close();
}
if (null != response)
{
response.Close();
}
if (null != dataStream)
{
dataStream.Close();
}
}
o_sRes = sRes;
return sMsg;
}
}
}