澳门新葡亰平台官网:.NET Http要求

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

如果代码运行在IKVM上,那么.NET开发者可以在Java代码中使用自定义属性。如果代码运行在IKVM上,那么Java与.NET这两个平台都可以使用属性。这样,开发者就可以在Java代码中使用.NET自定义属性,也可以在C#类与成员中使用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已完全支持Java 7,包括对新的指令invoke
dynamic的支持。然而,Frijters承认还有很多工作要做以提升新指令的性能。

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

在背后,IKVM的运行时与编译器主要都是使用C#编写的。Frijters可以使用公开的Java代码(来自于GNU
Classpath与OpenJDK等)并替换掉托管代码中的原生C代码。如果用户在应用中没有使用原生代码,那么使用了IKVM的Java应用就是完全
可移植的,因为IKVM并未使用任何原生代码。

发表评论

电子邮件地址不会被公开。 必填项已用*标注