private string GetResponse(string json) { //request = WebRequest.Create(url) as HttpWebRequest; String responseString; request.Method = "Post"; request.ContentType = "application/json;charset=UTF-8"; Stream stream = request.GetRequestStream(); byte[] buffer = Encoding.UTF8.GetBytes(json); stream.Write(buffer, 0, buffer.Length); HttpWebResponse response = request.GetResponse() as HttpWebResponse; if (response.StatusCode == HttpStatusCode.OK) { Stream responseStream = response.GetResponseStream(); using (StreamReader sr = new StreamReader(responseStream)) { responseString = sr.ReadToEnd(); } } else { throw new Exception(response.StatusDescription); } response.Close(); return responseString; } private void SetRequestHeaders(string tcHost, string tcUserName, string tcPassword, int tnTimeout, string url) { String fullUrl = tcHost + '|' + url; fullUrl = Regex.Replace(fullUrl, @"(/*\|/*)", "/"); request = WebRequest.Create(fullUrl) as HttpWebRequest; string authorizationKey = Convert.ToBase64String( System.Text.ASCIIEncoding.ASCII.GetBytes( string.Format("{0}:{1}", tcUserName, tcPassword))); request.Headers.Add("Authorization", "Basic " + authorizationKey); request.Timeout = tnTimeout * 1000; HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); request.CachePolicy = noCachePolicy; }Now, I have another question.
declare integer InitializeConnection in "dllNameHere x86.dll" string, string lnInitializeCode = InitializeConnection(.cODBC_DSN, .cDatabase)It uses the special attribute class
using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace DllExport { /// <summary> /// Attribute added to a static C# method to export it /// </summary> [AttributeUsage(AttributeTargets.Method)] public class DllExportAttribute : Attribute { private string m_exportName; private CallingConvention m_callingConvention; /// <summary> /// Constructor 1 /// </summary> /// <param name="exportName"></param> public DllExportAttribute(string exportName) : this(exportName, System.Runtime.InteropServices.CallingConvention.StdCall) { } /// <summary> /// Constructor 2 /// </summary> /// <param name="exportName"></param> /// <param name="callingConvention"></param> public DllExportAttribute(string exportName, CallingConvention callingConvention) { m_exportName = exportName; m_callingConvention = callingConvention; } /// <summary> /// Get the export name, or null to use the method name /// </summary> public string ExportName { get { return m_exportName; } } /// <summary> /// Get the calling convention /// </summary> public string CallingConvention { get { switch (m_callingConvention) { case System.Runtime.InteropServices.CallingConvention.Cdecl: return typeof(CallConvCdecl).FullName; case System.Runtime.InteropServices.CallingConvention.FastCall: return typeof(CallConvFastcall).FullName; case System.Runtime.InteropServices.CallingConvention.StdCall: return typeof(CallConvStdcall).FullName; case System.Runtime.InteropServices.CallingConvention.ThisCall: return typeof(CallConvThiscall).FullName; case System.Runtime.InteropServices.CallingConvention.Winapi: return typeof(CallConvStdcall).FullName; default: return ""; } } } } }and it's added this way
[DllExport("Test", System.Runtime.InteropServices.CallingConvention.StdCall)] public static String Test(String dataSource, String initialCatalog) { return dataSource + ' ' + initialCatalog; }So, I'm wondering if I should try making my class and methods static and use that same way.
>WebClient client = new WebClient(); >client.Headers.Set("Accept", "application/json"); >client.Headers.Set("Content-Type", "application/json"); > >// this should handle basic auth >client.Credentials = new NetworkCredential("username", "password"); > >var resultJson = client.UploadString(url,json); >>
> [TestMethod] > public void JsonRequestTest() > { > var settings = new HttpRequestSettings() > { > Url = "http://codepaste.net/recent?format=json", > Credentials = new NetworkCredential("username","Password") > }; > var snippets = HttpUtils.JsonRequest<List<CodeSnippet>>(settings); > > Assert.IsNotNull(snippets); > Assert.IsTrue(settings.ResponseStatusCode == System.Net.HttpStatusCode.OK); > Assert.IsTrue(snippets.Count > 0); > Console.WriteLine(snippets.Count); > } >>
>>private void SetRequestHeaders(string tcHost, string tcUserName, string tcPassword, int tnTimeout) >> { >> request = WebRequest.Create(tcHost) as HttpWebRequest; >> request.Host = tcHost; >> request.Headers.Remove("Accept"); >> request.Headers.Add("Accept", "application/json"); >> string authorizationKey = Convert.ToBase64String( >> System.Text.ASCIIEncoding.ASCII.GetBytes( >> string.Format("{0}:{1}", tcUserName, tcPassword))); >> request.Headers.Add("Authorization", "Basic " + authorizationKey); >> request.Timeout = tnTimeout * 1000; >> }>>
>>"/integrationapi//Attraction/Tickets/Validate"
>>>>>>static async Task<HttpResponseMessage> GetResponse(TicketUsageRequests ticket, string apiUrl) >>>> { >>>> HttpResponseMessage response = await client.PostAsJsonAsync(apiUrl, ticket); >>>> //response.EnsureSuccessStatusCode(); >>>> >>>> return response; >>>> }>>>>
>>>>try >>>> { >>>> SetRequestHeaders(tcHost, tcUserName, tcPassword, tnTimeout, tnRetries); >>>> >>>> TicketUsageRequests ticket = new TicketUsageRequests(); >>>> ticket.TicketBarcode = tcBarcode; >>>> ticket.TicketUsageDate = tcUsageDate; >>>> ticket.UserID = tcUserName; >>>> >>>> ... here I want to get the result of the response and parse it >>>> >>>> ... >>>>>>>>