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ì)有所幫助。
您可能感興趣的文章:
- c# 在WebBrowser中用SendMessage模擬鼠標(biāo)點(diǎn)擊
- C#模擬Http與Https請(qǐng)求框架類實(shí)例
- C#模擬http 發(fā)送post或get請(qǐng)求的簡(jiǎn)單實(shí)例
- C#采用mouse_event函數(shù)實(shí)現(xiàn)模擬鼠標(biāo)功能
- C#實(shí)現(xiàn)的三種模擬自動(dòng)登錄和提交POST信息的方法
- C# SendInput 模擬鼠標(biāo)操作的實(shí)現(xiàn)方法
- 使用C#發(fā)送Http請(qǐng)求實(shí)現(xiàn)模擬登陸實(shí)例
- c#模擬銀行atm機(jī)示例分享
- C#模擬window操作鼠標(biāo)的方法
- C#如何使用Bogus創(chuàng)建模擬數(shù)據(jù)示例代碼
相關(guān)文章
C#服務(wù)器NFS共享文件夾搭建與上傳圖片文件的實(shí)現(xiàn)
本文主要介紹了C#服務(wù)器NFS共享文件夾搭建與上傳圖片文件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
基于WPF實(shí)現(xiàn)帶明細(xì)的環(huán)形圖表
這篇文章主要介紹了如何利用WPF繪制帶明細(xì)的環(huán)形圖表?,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2022-08-08
C# Winfrom實(shí)現(xiàn)Skyline畫直線功能的示例代碼
這篇文章主要介紹了C# Winfrom實(shí)現(xiàn)Skyline畫直線功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
C# .NET及Mono跨平臺(tái)實(shí)現(xiàn)原理解析
這篇文章主要介紹了C# .NET及Mono、跨平臺(tái)實(shí)現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05

