1.问题描述:

HttpClint 使用FormUrlEncodedContent 调用接口时 报错 System.UriFormatException: 无效的 URI: URI 字符串太长;

2.解决:

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web; namespace PointToPoint
{
class Program
{
static void Main(string[] args)
{
string data = ""; byte[] buffer = System.Text.UTF8Encoding.UTF8.GetBytes(data);
//压缩后的byte数组
byte[] compressedbuffer = null;
//Compress buffer,压缩缓存
MemoryStream ms = new MemoryStream();
using (GZipStream zs = new GZipStream(ms, CompressionMode.Compress, true))
{
zs.Write(buffer, 0, buffer.Length);
}
//只有GZipStream在Dispose后调应对应MemoryStream.ToArray()所得到的Buffer才是我们需要的结果
compressedbuffer = ms.ToArray();
//将压缩后的byte数组basse64字符串
string text64 = Convert.ToBase64String(compressedbuffer);
var content = HttpUtility.UrlEncode(text64);
HttpClient client = new HttpClient();
string d = "account=帐号&password=密码&content=" + System.Web.HttpUtility.UrlEncode(text64); var contenStr = new StringContent(d, Encoding.UTF8);
contenStr.Headers.Remove("Content-Type");//必须
contenStr.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//1.根据需求设置
string rlt = client.PostAsync("http://192.168.1.82/BatchSms.ashx", contenStr).Result.Content.ReadAsStringAsync().Result;
//string rlt = client.SendAsync(request).Result.Content.ReadAsStringAsync().Result;
Console.WriteLine(rlt);
Console.ReadKey();
}
}
}

最新文章

  1. 恢复Reflector反编译后资源文件的办法
  2. android消息处理机制之2handler与looper,MessageQueue:的关系
  3. 链队列之C++实现
  4. 利用Vim提供的正则去掉代码每行开头不想要的行号以及vi常见问题和应用技巧
  5. Linux安装Python2.7.9
  6. Microsoft SQL Server 2016 RC3 安装
  7. Aquarium Tank(csu1634+几何+二分)Contest2087 - 湖南多校对抗赛(2015.05.24)-G
  8. ABAP 文件选择框
  9. js设计模式(六)---组合模式
  10. Convert java.lang.String to oracle.jbo.domain.Date
  11. 由异常掉电问题---谈xfs文件系统
  12. Android Camera开发:给摄像头预览界面加个ZoomBar(附完整代码下载)
  13. IRGAN:A Minimax Game for Unifying Generative and Discriminative Information Retrieval Models
  14. Java springboot项目的jar发布方式
  15. 有关mysql的innodb_flush_log_at_trx_commit参数【转】
  16. 莫队p2 【bzoj3809】Gty的二逼妹子序列
  17. JAX-WS使用Handler实现简单的WebService权限验证
  18. 数据结构(逻辑结构,物理结构,特点) C#多线程编程的同步也线程安全 C#多线程编程笔记 String 与 StringBuilder (StringBuffer) 数据结构与算法-初体验(极客专栏)
  19. Spring_通过注解配置 Bean(2)
  20. ujmp使用心得

热门文章

  1. Top 22 Free Responsive HTML5 Admin & Dashboard Templates 2018
  2. linux vim,vi编辑器的基础
  3. cgroups
  4. JavaScript--数据结构与算法之散列
  5. nodejs学习(三)--express连接mysql数据库,mysql查询封装
  6. android 弹幕评论效果
  7. 制作 wordpress 博客静态化到本地
  8. Uniform Server
  9. 26.多线程join detach
  10. ReactJs 入门DEMO(转自别人)