zoukankan      html  css  js  c++  java
  • 发送post数据

    static void Main(string[] args)
    {
    //Console.WriteLine("Hello World!");
    //ShowInt(10);
    //ShowString("test");
    //ShowDateTime(DateTime.Now);
    //ShowObject("福州测试");
    // Console.WriteLine(typeof(List<>));
    // Console.WriteLine(typeof(Dictionary<,>));

    //Show<int>(200);
    //Action<int> a = Test;
    //a.BeginInvoke(100,null,null);//开启一个新的线程去执行a所引用的方法
    //Console.WriteLine("main");
    //Console.ReadLine();


    //使用appdomain获取当前应用程序集的执行目录
    string dir = AppDomain.CurrentDomain.BaseDirectory;
    //使用path获取当前应用程序集的执行的上级目录
    dir = Path.GetFullPath("..");
    //使用path获取当前应用程序集的执行目录的上级的上级目录
    dir = Path.GetFullPath("../../../temp");

    string qwPath = dir + "/全文笔录.doc";
    string zyPath = dir + "/摘要笔录.doc";
    byte[] quanwenByte= File2Bytes(qwPath);
    byte[] zhaiyaoByte = File2Bytes(zyPath);
    string ZhaiyaoUrl = Convert.ToBase64String(zhaiyaoByte);
    string QuanwenUrl = Convert.ToBase64String(quanwenByte);

    string strjson ="{ "username":"小五","ZhaiyaoUrl":""+ ZhaiyaoUrl + "","QuanwenUrl":""+ QuanwenUrl + ""}";
    ClientRequest("https://localhost:44303/api/Order/Upload", strjson,"POST");

    }

    /// <summary>
    /// 将文件转换为byte数组
    /// </summary>
    /// <param name="path">文件地址</param>
    /// <returns>转换后的byte数组</returns>
    public static byte[] File2Bytes(string path)
    {
    if (!System.IO.File.Exists(path))
    {
    return new byte[0];
    }

    FileInfo fi = new FileInfo(path);
    byte[] buff = new byte[fi.Length];
    FileStream fs = fi.OpenRead();
    fs.Read(buff, 0, Convert.ToInt32(fs.Length));
    fs.Close();

    return buff;
    }

    /// <summary>
    /// 发送请求(get/post/http/https)
    /// </summary>
    /// <param name="Uri">请求地址</param>
    /// <param name="JsonStr">json数据</param>
    /// <param name="Method">请求方式POST/GET</param>
    /// <returns></returns>
    public static string ClientRequest(string Uri, string JsonStr, string Method = "POST")
    {
    try
    {
    var httpRequest = (HttpWebRequest)HttpWebRequest.Create(Uri);
    httpRequest.Method = Method;
    httpRequest.ContentType = "application/json";
    if (Method.ToLower() == "get")
    {
    httpRequest.ContentType = "application/x-www-form-urlencoded";
    }
    httpRequest.Proxy = null;
    httpRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
    httpRequest.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.8,zh-hk;q=0.6,ja;q=0.4,zh;q=0.2");
    httpRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    if (!string.IsNullOrEmpty(JsonStr))
    {
    using (var dataStream = new StreamWriter(httpRequest.GetRequestStream()))
    {
    dataStream.Write(JsonStr);
    dataStream.Flush();
    dataStream.Close();
    }
    }
    var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
    using (var dataStream = new StreamReader(httpResponse.GetResponseStream()))
    {
    var result = dataStream.ReadToEnd();
    return result;
    }
    }
    catch (Exception ex)
    {
    return "{"error":"" + ex.Message + ""}";
    }
    }

  • 相关阅读:
    CentOS 编译安装 MySQL5.7
    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var mysql 启动不了
    Linux里如何查找文件内容
    linux怎么模糊查找一个文件
    centos7下使用yum安装mysql
    centos下完全卸载mysql
    Linux下安装配置Nexus
    Linux下建立Nexus私服
    阿里云主机上安装jdk
    java war run
  • 原文地址:https://www.cnblogs.com/wugh8726254/p/14752737.html
Copyright © 2011-2022 走看看