欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-04 17:45:47
本文实例讲述了c#基于socket模拟http请求的方法。分享给大家供大家参考。具体实现方法如下: using system; using system.col...

本文实例讲述了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#程序设计有所帮助。