国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#基于socket模擬http請求的方法

2020-01-24 01:40:04
字體:
來源:轉載
供稿:網友

本文實例講述了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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 枣阳市| 西乡县| 南江县| 临潭县| 关岭| 龙井市| 宝山区| 南投县| 邢台县| 塘沽区| 驻马店市| 公安县| 合肥市| 法库县| 海林市| 三门县| 黄陵县| 芮城县| 郧西县| 乳山市| 澄江县| 宜州市| 芷江| 共和县| 景泰县| 满洲里市| 吉首市| 贡山| 奉新县| 沂源县| 鄢陵县| 神木县| 达尔| 澳门| 东乌珠穆沁旗| 手游| 铜山县| 汕头市| 东丽区| 剑川县| 阿合奇县|