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

 更新时间:2015年06月29日 15:43:59   作者:陈传文  
这篇文章主要介绍了C#基于socket模拟http请求的方法,实例分析了socket模拟http请求的实现技巧,需要的朋友可以参考下

本文实例讲述了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#实现将HTML转换成纯文本的方法

    C#实现将HTML转换成纯文本的方法

    这篇文章主要介绍了C#实现将HTML转换成纯文本的方法,基于自定义类实现文本转换功能,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • C#实现加密的几种方法介绍

    C#实现加密的几种方法介绍

    这篇文章介绍了C#实现加密的几种方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • 基于C#实现一个温湿度监测小工具

    基于C#实现一个温湿度监测小工具

    这篇文章主要为大家详细介绍了如何基于C#实现一个温湿度监测小工具,文中的示例代码讲解详细,具有一定的借鉴价值,需要的可以参考一下
    2023-01-01
  • C# 线性插值的实现示例

    C# 线性插值的实现示例

    线性插值是针对一维数据的插值方法,本文主要介绍了C# 线性插值的实现示例,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • 详解C#编程中异常的创建和引发以及异常处理

    详解C#编程中异常的创建和引发以及异常处理

    这篇文章主要介绍了C#编程中异常的创建和引发以及异常处理,文中介绍了Catch块和Finally块等基本的异常处理要点,需要的朋友可以参考下
    2016-02-02
  • C#位移的介绍与例子

    C#位移的介绍与例子

    很多人提问,不知道C#位移,可能有些人在面试中也遇到过
    2013-04-04
  • C#模拟链表数据结构的实例解析

    C#模拟链表数据结构的实例解析

    这篇文章主要介绍了C#模拟链表数据结构的实例解析,包括队双向链表的模拟方法,例子中队链表的操作也有很好的说明,需要的朋友可以参考下
    2016-04-04
  • 深入委托与多播委托的详解

    深入委托与多播委托的详解

    本篇文章是对委托与多播委托进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • C#调用webservice接口的最新方法教程

    C#调用webservice接口的最新方法教程

    webservice 可以用于分布式应用程序之间的交互,和不同程序之间的交互。下面这篇文章主要给大家介绍了关于C#调用webservice接口的相关资料,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友下面随着小编来一起看看吧。
    2017-11-11
  • C# 获取某个时间的0点0分和23点59分59秒

    C# 获取某个时间的0点0分和23点59分59秒

    这篇文章主要介绍了C# 获取某个时间的0点0分和23点59分59秒,文中给大家提到了java 获取某一日期的0点0分0秒和23点59分59秒,需要的朋友可以参考下
    2019-09-09

最新评论