C#调用接口注意要点 socket,模拟服务器、客户端通信 在ASP.NET Core中构建路由的5种方法...

C#调用接口注意要点

 

在用C#调用接口的时候,遇到需要通过调用登录接口才能调用其他的接口,因为在其他的接口需要在登录的状态下保存Cookie值才能有权限调用,

所以首先需要通过调用登录接口来保存cookie值,再进行其他接口的调用

1.通过Get方式

复制代码
        #region get方式

        public string HttpGet(string url)
        {

            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "application/json";
            request.Headers["Accept-Encoding"] = "gzip,deflase";
            request.AutomaticDecompression = DecompressionMethods.GZip;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            // HttpCookie cookies = new HttpCookie("admin");  //如果有需要通过登录实现保存cookie值的话可以加一部分
            // cookies.Value = Convert.ToString(response.Headers["Set-Cookie"]); // 通过响应请求读取带cookie的http数据
            // cookies.Expires = DateTime.Now.AddDays(1);
            //  HttpContext.Current.Response.Cookies.Add(cookies);

            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }
        #endregion
复制代码

 

但是并不是所有的get请求都需要添加这个heard的内容,有些加了这个可能出现乱码的情况,所以不要设置Accept-Encoding的Header

此处之所以加此header,是因为看到网页分析工具中所得到的浏览器浏览该网页,对应的http的header的内容中,就是这样设置的。

所以,代码中,也是模拟浏览器去访问网页,就设置了对应的Accept-Encoding为gzip,deflate了

普通浏览器访问网页,之所以添加:"Accept-Encoding" = "gzip,deflate"

那是因为,浏览器对于从服务器中返回的对应的gzip压缩的网页,会自动解压缩,所以,其request的时候,添加对应的头,表明自己接受压缩后的数据。

同时添加了 request.AutomaticDecompression = DecompressionMethods.GZip;这一句,便可以获得正确的数据。

如果你获取网页内容太大的话,那么还是可以用这个办法的,这样就可以让HttpWebRequest自动帮你实现对应的解压缩了,可以减少数据数据传输量,节省时间,提高效率。

2.通过post方式

复制代码
public string HttpPost2(string url, string body)
{

   //把用户传过来的数据转成“UTF-8”的字节流
    Encoding encoding = Encoding.UTF8;
    //先根据用户请求的uri构造请求地址
    //创建Web访问对象
    HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
    request.Method = "POST";
    // request.Accept = "application/json";
   request.ContentType = "application/json; charset=UTF-8";
   request.Headers["Accept-Encoding"] = "gzip, deflate";
   request.AutomaticDecompression = DecompressionMethods.GZip;
   //HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies["admin"];  //若是需要登录过后再能访问获取url的数据,需要在请求头中设置cookie值
   //if (Cookie != null)
   //    request.Headers.Add("Cookie", Cookie.Value.ToString());

   byte[] buffer = encoding.GetBytes(body);
   request.ContentLength = buffer.Length;
   request.GetRequestStream().Write(buffer, 0, buffer.Length);
   //通过Web访问对象获取响应内容
   HttpWebResponse response = (HttpWebResponse) request.GetResponse();
   //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
   using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
   {
    return reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
   }
 }
复制代码

3.通过put请求

复制代码
        #region Put请求
        public string Put(string data, string uri)
        {//创建Web访问对象
            HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(uri);
            //把用户传过来的数据转成“UTF-8”的字节流
            byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);

            Request.Method = "PUT";
            Request.ContentLength = buf.Length;
            Request.ContentType = "application/json";
            Request.MaximumAutomaticRedirections = 1;
            Request.AllowAutoRedirect = true;
            //发送请求
            Stream stream = Request.GetRequestStream();
            stream.Write(buf, 0, buf.Length);
            stream.Close();

            //获取接口返回值
            //通过Web访问对象获取响应内容
            HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
            //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
            StreamReader reader = new StreamReader(Response.GetResponseStream(), Encoding.UTF8);
            //string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
            string returnXml = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
            reader.Close();
            Response.Close();
            return returnXml;

        }           
        #endregion
复制代码

4.通过Delete请求

复制代码
        #region Delete请求
        public string Delete(string data, string uri)
        {
            //创建Web访问对象
            HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(uri);
            //把用户传过来的数据转成“UTF-8”的字节流
            byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);

            Request.Method = "DELETE";
            Request.ContentLength = buf.Length;
            Request.ContentType = "application/json";
            Request.MaximumAutomaticRedirections = 1;
            Request.AllowAutoRedirect = true;
            //发送请求
            Stream stream = Request.GetRequestStream();
            stream.Write(buf, 0, buf.Length);
            stream.Close();

            //获取接口返回值
            //通过Web访问对象获取响应内容
            HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
            //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
            StreamReader reader = new StreamReader(Response.GetResponseStream(), Encoding.UTF8);
            //string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
            string returnXml = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
            reader.Close();
            Response.Close();
            return returnXml;

        }
       #endregion
复制代码

不同的场景需求,使用不同的方式,应用在不同的场景 。

通过这几种组合方式 ,可以调用http接口,完成调用和测试。

 

 

socket,模拟服务器、客户端通信

服务器代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Missuin
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//当点击开始监听的时候 在服务器端创建一个负责监IP地址和端口号的Socket
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
IPAddress ip = IPAddress.Any;//IPAddress.Parse(txtServer.Text);
//创建端口号对象
IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text));
//监听
socket.Bind(point);
ShowMsg("监听成功");
socket.Listen(10);

Thread th = new Thread(Listen);
th.IsBackground = true;
th.Start(socket);
}


Socket socketSend;
/// <summary>
/// 被线程所执行的函数,只能传object参数
/// </summary>
/// <param name="o"></param>
void Listen(Object o)
{
Socket socketWatch = o as Socket;
//等待客户端连接 并创建一个负责通信的Sokcet
while (true)
{
//负责根客户通信的Socket
socketSend = socketWatch.Accept();
dictSocket.Add(socketSend.RemoteEndPoint.ToString(), socketSend);

cbuUsers.Items.Add(socketSend.RemoteEndPoint.ToString());
ShowMsg(socketSend.RemoteEndPoint.ToString() + ":" + "连接成功");
客户端连接成功后,服务器应该接受客户端发来的消息
//byte[] buffer = new byte[1024 * 1024 * 2];
实际接受到的有效字节数
//int r = socketSend.Receive(buffer);
//string str = Encoding.UTF8.GetString(buffer, 0, r);
//Console.WriteLine(socketSend.RemoteEndPoint+":"+str);
Thread th = new Thread(Recive);
th.IsBackground = true;
th.Start(socketSend);
}
}

Dictionary<String, Socket> dictSocket = new Dictionary<string, Socket>();
/// <summary>
/// 服务器端不修改的接收客户发来的消息
/// </summary>
/// <param name="o"></param>
void Recive(object o)
{
Socket socketSend = o as Socket;
while (true)
{
try
{
//客户端连接成功后,服务器应该接受客户端发来的消息
byte[] buffer = new byte[1024 * 1024 * 2];
//实际接受到的有效字节数
int r = socketSend.Receive(buffer);
if (r == 0)
break;
string str = Encoding.UTF8.GetString(buffer, 0, r);
ShowMsg(socketSend.RemoteEndPoint + ":" + str);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}

void ShowMsg(string msg)
{
txtLog.AppendText(msg + "\r\n");
}

private void Form2_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
}
/// <summary>
/// 服务器给客户端发消息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
byte[] msgs = Encoding.UTF8.GetBytes(this.txtmsg.Text);

List<byte> list = new List<byte>();
list.Add(0);
list.AddRange(msgs);
byte[] newmsgs = list.ToArray();
//获得用户在下拉框中选中的IP地址
string ip = cbuUsers.SelectedItem.ToString();
Socket sendSocket = dictSocket[ip];
sendSocket.Send(newmsgs);
ShowMsg(sendSocket.RemoteEndPoint + ":" + this.txtmsg.Text);

}
/// <summary>
/// 选择要发送的文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSelect_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.InitialDirectory = @"C:\users";
of.Title = "请选择要发送的文件";
of.Filter = "所有文件|*.*";
of.ShowDialog();

txtPath.Text = of.FileName;

}

private void btnFileSend_Click(object sender, EventArgs e)
{
string path = this.txtPath.Text.ToString();
using(FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[1024 * 1024 * 5];
List<byte> list = new List<byte>();
list.Add(1);
list.AddRange(list);
byte[] newBytes = list.ToArray();
Socket socket = dictSocket[cbuUsers.SelectedItem.ToString()];
socket.Send(newBytes);
}

private void button3_Click(object sender, EventArgs e)
{
byte[] buffer = new byte[1];
buffer[0] = 2;
Socket socket = dictSocket[cbuUsers.SelectedItem.ToString()];
socket.Send(buffer);
}
}
}

客户端代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//创建负责通信的Socket
Socket socketSend;
private void button1_Click(object sender, EventArgs e)
{
try
{
socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse(textBox1.Text);
IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(textBox2.Text));
socketSend.Connect(point);
showMsg("连接成功");

//开启一个新线程不停的接收服务器发来的消息
Thread th = new Thread(recive);
th.IsBackground = true;
th.Start();
}
catch(Exception ex)
{


}

/// <summary>
/// 不停的接收服务器发来的消息
/// </summary>
void recive()
{
while (true)

byte[] buffer = new byte[1024 * 1024 * 2];
int r = socketSend.Receive(buffer);
if (r == 0)
break;
//发来的是文字
if (buffer[0] == 0)
{
string s = Encoding.UTF8.GetString(buffer,1,r-1);
showMsg(socketSend.RemoteEndPoint + ":" + s);
}
else if (buffer[0] == 1)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.InitialDirectory = @"C:\";
sfd.Title = "请选择要保存的文件";
sfd.Filter = "所有文件|*.*";
sfd.ShowDialog(this);
string path = sfd.FileName;
using(FileStream sf = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
sf.Write(buffer, 1, r - 1);
}
MessageBox.Show("保存成功");

else if (buffer[0] == 2)
{
for (int i=0;i<10;i++){
this.Location = new Point(200, 200);
this.Location = new Point(210, 210);

}
}
}

private void showMsg(string msg)
{
this.txtLog.AppendText(msg + "\r\n");
}

private void button2_Click(object sender, EventArgs e)
{
byte[] msgs = System.Text.Encoding.UTF8.GetBytes(txtmsg.Text);
socketSend.Send(msgs);
}

private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
}
}
}

 

 

 

在ASP.NET Core中构建路由的5种方法

原文链接 :https://stormpath.com/blog/routing-in-asp-net-core 

在ASP.NET Core中构建路由的5种方法 

 

 

by Team Stormpath | August 17, 2016 | 

 

在软件开发中,路由用于将所有传入请求映射到处理程序,并生成响应中使用的URL。在ASP.NET Core中,路由已经从根本上重写了。以前,使用MVC和Web API进行路由非常相似,但两者都使用不同的框架(和代码)来执行相同的操作。一个重要的区别是Web API默认支持RESTful路由。例如,如果一个控制器的操作方法名称开头Post,那么调用一个HTTP Post默认情况下会调用该方法。 

由于微软决定重建和统一路由框架,现在适用于MVC,也适用于Web API。然而,在我们深入了解如何构建路由之前,让我们回顾一下为什么路由对您的应用程序非常重要。 

为什么路由? 

SEO友好 

REST式配置的路由有助于您的内容的搜索引擎优化(SEO)。网站的网址是影响网站排名的首要标准之一。通过将www.yourwebsite.com/articles/show/123转换为www.yourwebsite.com/how-to-peel-potatoes,您鼓励搜索引擎对与“how to peel potatoes.”有关的关键语句进行排名。 

此外,如果您的网址具有更强的描述性,则用户可以更轻松地预测内容,从而增加页面上的时间,这也会影响SEO和整体页面权限。 

网址不需要映射文件 

没有路由,传入的请求将被映射到物理文件。通过路由,我们可以完全控制请求,使我们能够决定在某个HTTP请求进入时我们执行的操作和控制器。 

长URL和文件扩展名可以省略 

在许多参数和过滤器都在使用的情况下,路由有助于缩短URL。通过消除文件扩展名,我们可以隐藏我们正在工作的环境。 

那么,我们如何利用这些好处呢?让我们看看您可以在ASP.NET Core应用程序中构建路由的五种方法。 

1.创建默认路由 

您可以按照惯例在您的项目Startup类中定义默认路由。 

 

1 

2 

3 

4 

5 

6 

7 

8 

9 

10 

11 

12 

13 

14 

15 

16 

17 

18 

public class Startup 

{ 

    public void ConfigureServices(IServiceCollection services) 

    { 

        services.AddMvc(); 

    } 

  

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 

    { 

        app.UseMvc(routes => 

        { 

            routes.MapRoute( 

                name: "default", 

                template: "  {controller=Home}/{action=Index}/{id?}"); 

        }); 

    } 

} 

通过以上,我们确保我们项目中的基本配置存在于Controller + Action + ID(可选)路径的标准MVC模式中。你也可以像这样声明路由模式: 

 

1 

2 

3 

4 

5 

6 

routes.MapRoute( 

    name: "default_route", 

    template: "{controller}/{action}/{id?}", 

    defaults: new { controller = "Home", action = "Index" } 

); 

(这是我们用来在ASP.NET Core中进行路由的方式。) 

2.扩展默认路由 

一旦我们配置了默认路由,我们可能希望通过根据特定需求添加自定义路由来扩展它。为此,我们可以使用该MapRoute()方法添加配置。 

 

1 

2 

3 

4 

5 

6 

7 

8 

9 

10 

11 

12 

13 

14 

app.UseMvc(routes => 

{ 

    //New Route 

    routes.MapRoute( 

       name: "about-route", 

       template: "about", 

       defaults: new { controller = "Home", action = "About" } 

    ); 

  

routes.MapRoute( 

    name: "default", 

    template: "{controller=Home}/{action=Index}/{id?}"); 

}); 

我们添加了一条额外的路线,通过路线授予对主控制器上“关于”操作的访问权限/about。由于默认模式路由仍然存在,因此我们也可以使用常规/home/about路由访问“关于”页面。 

3.使用属性 

您还可以使用控制器中的属性和操作来配置路由。 

 

1 

2 

3 

4 

5 

6 

7 

8 

9 

10 

11 

12 

13 

14 

15 

16 

[Route("[controller]")] 

public class AnalyticsController : Controller 

{ 

    [Route("Dashboard")] 

    public IActionResult Index() 

    { 

        return View(); 

    } 

  

    [Route("[action]")] 

    public IActionResult Charts() 

    { 

        return View(); 

    } 

} 

在本示例中,我们可以通过以下路径访问控制器操作: 

  • /Analytics/Dashboard 

  • /Analytics/Charts 

您可以看到这两个标记,[controller][action]指出我们必须引用已声明的控制器和操作名称。在这种情况下,“Analytics”是控制器的名称,“Charts”是动作的名称,因此它是路由的名称。 

4.构建RESTful路线 

为了声明一个RESTful控制器,我们需要使用以下路由配置: 

 

1 

2 

3 

4 

5 

6 

7 

8 

9 

10 

11 

12 

13 

14 

15 

16 

17 

[Route("api/[controller]")] 

public class ValuesController : Controller 

{ 

    // GET api/values 

    [HttpGet] 

    public IEnumerable<string> Get() 

    { 

        return new string[] {"hello", "world!"}; 

    } 

  

    // POST api/values 

    [HttpPost] 

    public void PostCreate([FromBody] string value) 

    { 

    } 

} 

在这里,我们告诉我们的RESTful服务接受/api/values路由下的呼叫。请注意,我们不再使用该Route属性进行操作。相反,我们与装饰它HttpGetHttpPostHttpPutHttpDelete的属性。 
或者,我们可以看看不同的场景: 

 

1 

2 

3 

4 

5 

6 

// POST api/values/5 

[HttpPost("{id}")] 

public void PostUpdate(int id, [FromBody] string value) 

{ 

} 

在这里,我们有以下路线的控制器值 

  • HTTP Post/values路线将调用Post()行动 

  • HTTP Post/values/PostName路线将调用Post([FromBody]string value)行动 

5.使用约束 

我们可以将使用约束传递给动作的值的类型进行限制。例如,如果我们期望一个参数是一个数字,我们必须将其限制为整数类型。使用大括号在属性中声明约束{id:int} 

 

1 

2 

3 

4 

5 

6 

[HttpGet("{id:int}")] 

public string GetById(int id) 

{ 

    return "item " + id; 

} 

在这里,我们告诉动作GetByID只接受一个整数参数。向约束添加问号{id:int?}表示该参数是可选的。因此,如果有问号,我们可以打电话/GetByID/123/GetByID不带附加参数。我们也可以这样定义在Startup类中声明的默认路由中的约束: 

 

1 

2 

3 

4 

5 

routes.MapRoute( 

    name: "getProductById", 

    template: "Products/{id:int}", 

    defaults: new { controller = "Products", action = "GetById" }); 

滴水能把石穿透,成事功到自然成

转载于:https://www.cnblogs.com/cjm123/p/10745756.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个问题我可以回答。在.NET Core(C#)使用EPPlus.Core导出Excel文档,可以按照以下步骤进行: 1. 首先需要安装EPPlus.Core库,可以通过NuGet包管理器或者手动下载安装。 2. 在代码引入EPPlus.Core库的命名空间,如下所示: ``` using OfficeOpenXml; using OfficeOpenXml.Style; ``` 3. 创建Excel文档,并设置表头和数据。可以参考以下示例代码: ``` var file = new FileInfo("example.xlsx"); using (var package = new ExcelPackage(file)) { var worksheet = package.Workbook.Worksheets.Add("Sheet1"); // 设置表头 worksheet.Cells[1, 1].Value = "列1"; worksheet.Cells[1, 2].Value = "列2"; worksheet.Cells[1, 3].Value = "列3"; // 设置数据 for (int i = 2; i <= 100; i++) { worksheet.Cells[i, 1].Value = i - 1; worksheet.Cells[i, 2].Value = "数据" + (i - 1); worksheet.Cells[i, 3].Value = DateTime.Now.AddDays(i - 1); } // 设置单元格样式 worksheet.Cells[1, 1, 1, 3].Style.Font.Bold = true; worksheet.Cells[1, 1, 1, 3].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells[1, 1, 1, 3].Style.Fill.BackgroundColor.SetColor(Color.LightGray); // 保存Excel文档 package.Save(); } ``` 4. 如果需要在导出Excel文档的过程显示进度条,可以使用异步方法和进度报告器。参考以下示例代码: ``` var file = new FileInfo("example.xlsx"); using (var package = new ExcelPackage(file)) { var worksheet = package.Workbook.Worksheets.Add("Sheet1"); // 设置表头 worksheet.Cells[1, 1].Value = "列1"; worksheet.Cells[1, 2].Value = "列2"; worksheet.Cells[1, 3].Value = "列3"; // 设置数据 for (int i = 2; i <= 100; i++) { worksheet.Cells[i, 1].Value = i - 1; worksheet.Cells[i, 2].Value = "数据" + (i - 1); worksheet.Cells[i, 3].Value = DateTime.Now.AddDays(i - 1); // 报告进度 var progress = (double)(i - 1) / 99; progressReporter.Report(progress); } // 设置单元格样式 worksheet.Cells[1, 1, 1, 3].Style.Font.Bold = true; worksheet.Cells[1, 1, 1, 3].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells[1, 1, 1, 3].Style.Fill.BackgroundColor.SetColor(Color.LightGray); // 保存Excel文档 await package.SaveAsync(); } ``` 以上就是在.NET Core(C#)使用EPPlus.Core导出Excel文档,并显示进度条的方法。希望能够对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值