C#基于socket模拟http请求的方法

作者:陈传文 时间:2022-09-12 09:55:11 

本文实例讲述了C#基于socket模拟http请求的方法。分享给大家供大家参考。具体实现方法如下:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
class HttpHelper
{
 #region 模拟客户端socket连接
 private static Socket ConnectSocket(string server, int port)
 {
  Socket s = null;
  IPHostEntry hostEntry = null;
  // Get host related information.
  hostEntry = Dns.GetHostEntry(server);
  // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
  // an exception that occurs when the host IP Address is not compatible with the address family
  // (typical in the IPv6 case).
  foreach (IPAddress address in hostEntry.AddressList)
  {
   IPEndPoint ipe = new IPEndPoint(address, port);
   Socket tempSocket =
   new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
   tempSocket.Connect(ipe);
   if (tempSocket.Connected)
   {
    s = tempSocket;
    break;
   }
   else
   {
    continue;
   }
  }
  return s;
 }
 #endregion
 #region 请求的主方法 request 是http请求的头部,可以用抓包工具获取,server可以使域名或者是ip地址,port http协议一般是80
 public static string SocketSendReceive(string request, string server, int port)
 {
  try
  {
   Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
   Byte[] bytesReceived = new Byte[655350];
   // 创建连接
   Socket s = ConnectSocket(server, port);
   if (s == null)
    return ("Connection failed");
   // 发送内容.
   s.Send(bytesSent, bytesSent.Length, 0);
   // Receive the server home page content.
   int bytes = 0;
   string page = "Default HTML page on " + server + ":\r\n";
   //接受返回的内容.
   do
   {
    bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
    page = page + Encoding.UTF8.GetString(bytesReceived, 0, bytes);
   }
   while (bytes > 0);

return page;
  }
  catch
  {
   return string.Empty;
  }
 }
 #endregion
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
class Program
{
 public static string HeadlerInit() {
  StringBuilder sb = new StringBuilder();
  sb.AppendLine("GET http://www.baidu.com/ HTTP/1.1");
  sb.AppendLine("Host: www.baidu.com");
  sb.AppendLine("Connection: keep-alive");
  sb.AppendLine("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
  sb.AppendLine("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36");
  sb.AppendLine("Accept-Encoding:deflate, sdch");
  sb.AppendLine("Accept-Language: zh-CN,zh;q=0.8");
  sb.AppendLine("\r\n");
  //这个一定要有不然接收回来可能没有数据
  return sb.ToString();
 }
 static void Main(string[] args)
 {
  string getStrs=HeadlerInit();
  string getHtml = HttpHelper.SocketSendReceive(getStrs, "www.baidu.com", 80);
  Console.WriteLine(getHtml);
 }
}

希望本文所述对大家的C#程序设计有所帮助。

标签:C#,socket,http
0
投稿

猜你喜欢

  • java编程多线程并发处理实例解析

    2022-06-02 22:14:59
  • Springboot自带定时任务实现动态配置Cron参数方式

    2023-11-10 10:21:31
  • SpringBoot使用Thymeleaf模板引擎访问静态html的过程

    2023-11-25 10:04:44
  • java 多态性详解及简单实例

    2021-10-06 23:29:50
  • 用Rational Rose逆向工程(java)生成类图(教程和错误解决)

    2023-05-20 20:34:14
  • Java实现顺序栈原理解析

    2021-08-26 15:47:16
  • Java 高并发十: JDK8对并发的新支持详解

    2022-12-02 02:43:09
  • Java实现简单的斗地主游戏

    2023-03-13 10:42:57
  • Android实现蓝牙串口通讯

    2023-03-19 09:22:21
  • MyBatis源码解析——获取SqlSessionFactory方式

    2023-04-03 15:09:14
  • Android Data Binding数据绑定详解

    2023-05-07 14:54:39
  • mvc开启gzip压缩示例分享

    2022-05-03 08:34:29
  • Flutter中数据库的使用教程详解

    2023-12-09 11:16:28
  • Mybatis-Plus自动填充更新操作相关字段的实现

    2023-06-04 22:37:12
  • c#批量上传图片到服务器示例分享

    2022-09-12 10:16:24
  • Android自定义SwipeRefreshLayout高仿微信朋友圈下拉刷新

    2023-01-06 08:51:34
  • Android利用FlexboxLayout轻松实现流动布局

    2021-06-24 02:41:53
  • C# 设计模式系列教程-外观模式

    2023-01-08 18:44:40
  • C# 重写ComboBox实现下拉任意组件的方法

    2022-01-24 03:07:29
  • Java多个版本切换的几种方法

    2022-04-22 14:10:15
  • asp之家 软件编程 m.aspxhome.com