欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#基于socket模擬http請(qǐng)求的方法

 更新時(shí)間:2015年06月29日 15:43:59   作者:陳傳文  
這篇文章主要介紹了C#基于socket模擬http請(qǐng)求的方法,實(shí)例分析了socket模擬http請(qǐng)求的實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了C#基于socket模擬http請(qǐng)求的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

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 請(qǐng)求的主方法 request 是http請(qǐng)求的頭部,可以用抓包工具獲取,server可以使域名或者是ip地址,port http協(xié)議一般是80
  public static string SocketSendReceive(string request, string server, int port)
  {
   try
   {
    Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
    Byte[] bytesReceived = new Byte[655350];
    // 創(chuàng)建連接
    Socket s = ConnectSocket(server, port);
    if (s == null)
     return ("Connection failed");
    // 發(fā)送內(nèi)容.
    s.Send(bytesSent, bytesSent.Length, 0);
    // Receive the server home page content.
    int bytes = 0;
    string page = "Default HTML page on " + server + ":\r\n";
    //接受返回的內(nèi)容.
    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");
   //這個(gè)一定要有不然接收回來可能沒有數(shù)據(jù)
   return sb.ToString();
  }
  static void Main(string[] args)
  {
   string getStrs=HeadlerInit();
   string getHtml = HttpHelper.SocketSendReceive(getStrs, "www.baidu.com", 80);
   Console.WriteLine(getHtml);
  }
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • C#中事務(wù)處理和非事務(wù)處理方法實(shí)例分析

    C#中事務(wù)處理和非事務(wù)處理方法實(shí)例分析

    這篇文章主要介紹了C#中事務(wù)處理和非事務(wù)處理方法,較為詳細(xì)的分析了C#中事務(wù)處理與非事務(wù)處理的使用技巧,對(duì)于使用C#進(jìn)行數(shù)據(jù)庫程序開發(fā)有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • 一文搞懂C#實(shí)現(xiàn)讀寫文本文件中的數(shù)據(jù)

    一文搞懂C#實(shí)現(xiàn)讀寫文本文件中的數(shù)據(jù)

    這篇文章重點(diǎn)給大家介紹C#實(shí)現(xiàn)讀寫文本文件中的數(shù)據(jù)的一些知識(shí),讀取.txt文件數(shù)據(jù)的實(shí)例代碼及寫入讀取過程完整代碼,感興趣的朋友跟隨小編一起看看吧
    2021-06-06
  • C#給Word不同頁面設(shè)置不同背景

    C#給Word不同頁面設(shè)置不同背景

    這篇文章主要介紹了C#給Word不同頁面設(shè)置不同背景,文章圖文講解的很清晰,有對(duì)于這方面不懂得同學(xué)可以學(xué)習(xí)下
    2021-02-02
  • c# 使用谷歌身份驗(yàn)證GoogleAuthenticator的示例

    c# 使用谷歌身份驗(yàn)證GoogleAuthenticator的示例

    這篇文章主要介紹了c# 使用谷歌身份驗(yàn)證GoogleAuthenticator的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2021-01-01
  • C# 將字節(jié)流轉(zhuǎn)換為圖片的實(shí)例方法

    C# 將字節(jié)流轉(zhuǎn)換為圖片的實(shí)例方法

    C# 將字節(jié)流轉(zhuǎn)換為圖片的實(shí)例方法,需要的朋友可以參考一下
    2013-03-03
  • C#中參數(shù)的傳遞方式詳解

    C#中參數(shù)的傳遞方式詳解

    本文詳細(xì)講解了C#中參數(shù)的傳遞方式,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • C# CheckedListBox控件的用法總結(jié)

    C# CheckedListBox控件的用法總結(jié)

    本篇文章主要介紹了C# CheckedListBox控件的用法總結(jié),想要學(xué)習(xí)CheckedListBox的同學(xué)可以了解一下。
    2016-12-12
  • 淺談Unity腳本生命周期與執(zhí)行順序

    淺談Unity腳本生命周期與執(zhí)行順序

    在Unity中,腳本可以理解為附加在游戲?qū)ο笊系挠糜诙x游戲?qū)ο笮袨榈闹噶畲a。必須綁定在游戲?qū)ο笊喜拍荛_始它的生命周期。游戲?qū)ο罂梢岳斫鉃槟苋菁{各種組件的容器,游戲?qū)ο蟮乃薪M件一起決定了這個(gè)對(duì)象的行為和游戲中的表現(xiàn)
    2021-06-06
  • C#將隱私信息(銀行賬戶,身份證號(hào)碼)中間部分特殊字符替換成*

    C#將隱私信息(銀行賬戶,身份證號(hào)碼)中間部分特殊字符替換成*

    大家在銀行交易某些業(yè)務(wù)時(shí),都可以看到無論是身份證、銀行賬號(hào)中間部分都是用*號(hào)替換的,下面這篇文章主要介紹C#將隱私信息(銀行賬戶,身份證號(hào)碼)中間部分特殊字符替換成*的相關(guān)資料,需要的朋友可以參考下
    2015-08-08
  • Unity Shader實(shí)現(xiàn)動(dòng)態(tài)霧效果

    Unity Shader實(shí)現(xiàn)動(dòng)態(tài)霧效果

    這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)動(dòng)態(tài)霧效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04

最新評(píng)論