程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#基於socket模仿http要求的辦法

C#基於socket模仿http要求的辦法

編輯:C#入門知識

C#基於socket模仿http要求的辦法。本站提示廣大學習愛好者:(C#基於socket模仿http要求的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#基於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.百度.com/ HTTP/1.1");
   sb.AppendLine("Host: www.百度.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.百度.com", 80);
   Console.WriteLine(getHtml);
  }
}

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved