俄罗斯贵宾会-俄罗斯贵宾会官网
做最好的网站

在.NET/Mono上运行Java俄罗斯贵宾会:Jeroens Frijters谈IKVM.NET

声明:本代码只是我使用的网络请求方式的封装,大家如果有其他的可以一起讨论讨论。

IKVM.NET(IKVM)是个面向.NET与Mono平台的JVM,用户可以通过它直接运行Java应用。近日俄罗斯贵宾会,,来自微软的Erik Meijer与IKVM创建者Jeroen Frijters讨论了该项目的几个方面。此次讨论的涉及面非常广,从IKVM的内核谈到了该项目的未来目标。其要点如下所示:

     本代码可以在.NET 与.NET CORE的平台下无须做任何改动(除非手动加一些必要的引用,resharper会有提示.)即可使用。

Frijters从2002年该项目启动开始就作为项目的主开发者。此次讨论首先从Frijters介绍IKVM是如何成为Mono项目的一部分 (形成一种双赢的局面,两个项目可以彼此贡献补丁)开始。从架构上来说,IKVM在.NET上完全实现出了Java的内存模型。Frijters尝试尽量 不使用包装器,在某些情况下需要通过包装器才能正确地处理Java的字符串接口。

using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;

namespace FrameWork.Common.Web
{
    public class HttpHelper
    {
        /// <summary>
        /// 发起一个HTTP请求(以POST同步方式)
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="param">参数:json格式字符串</param>
        public static string HttpPost(string url, string param = "")
        {
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.Accept = "*/*";
            request.Timeout = 10000;
            request.AllowAutoRedirect = false;
            StreamWriter requestStream = null;
            WebResponse response = null;
            string responseStr = null;
            try
            {
                requestStream = new StreamWriter(request.GetRequestStream());
                requestStream.Write(param);
                requestStream.Close();
                response = request.GetResponse();
                if (response != null)
                {
                    var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    responseStr = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                request = null;
                requestStream = null;
                response = null;
            }
            return responseStr;
        }

        /// <summary>
        /// get请求,同步方式
        /// </summary>
        public static string HttpGet(string url, string postDataStr)
        {
            var request = (HttpWebRequest)WebRequest.Create(url + (postDataStr == "" ? "" : "?") + postDataStr);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";

            var response = (HttpWebResponse)request.GetResponse();
            var myResponseStream = response.GetResponseStream();
            var myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            var retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }

        /// <summary>
        /// 发送短信,异步请求
     /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="jsonString">json字符串,手机号和短信内容必填</param>
        public static void SendMessage(string url, string jsonString)
        {
            var client = new HttpClient();
            HttpContent httpContext = new StringContent(jsonString, Encoding.UTF8);
            httpContext.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            //await异步等待回应
            client.PostAsync(url, httpContext).ContinueWith(t =>
            {
                // Get HTTP response from completed task. 
                var response = t.Result;
                // Check that response was successful or throw exception 
                response.EnsureSuccessStatusCode();
                // Read response asynchronously as JsonValue and write out top facts for each country 
                response.Content.ReadAsStringAsync().ContinueWith(
                    readTask =>
                    {
                        Console.WriteLine(readTask.Result + " time:==>" + DateTime.Now);

                    });
            });
        }
    }
}

如果代码运行在IKVM上,那么.NET开发者可以在Java代码中使用自定义属性。如果代码运行在IKVM上,那么Java与.NET这两个平台都可以使用属性。这样,开发者就可以在Java代码中使用.NET自定义属性,也可以在C#类与成员中使用Java注解。

本文由俄罗斯贵宾会发布于编程,转载请注明出处:在.NET/Mono上运行Java俄罗斯贵宾会:Jeroens Frijters谈IKVM.NET

您可能还会对下面的文章感兴趣: