private void button1_Click(object sender, EventArgs e)
{
string urlStr = this.textUrl.Text ;
if (urlStr == null || "".Equals(urlStr))
{
MessageBox.Show( "必须填写要防问的地址!");
return;
}
int port = 80;
try
{
port = Convert.ToInt32(this.textPort.Text);
}
catch
{
MessageBox.Show( "请填写正确的端口");
}
//可以为空
string path = "/"+this.textFile.Text;
this.textBox1.Text = "";
this.conn.StartAccess(urlStr, port, path, this.textBox1);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
class MyConnection
{
public void StartAccess( string urlStr ,int port , string path , TextBox list )
{
byte[] data = new byte[1024];
IPHostEntry gist = Dns.GetHostByName( urlStr );
//得到所访问的网址的IP地址
IPAddress ip = gist.AddressList[0];
IPEndPoint ipEnd = new IPEndPoint(ip, port);
//使用tcp协议 stream类型 (IPV4)
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
socket.Connect(ipEnd);
}
catch (SocketException e)
{
MessageBox.Show("与访问的服务器连接异常!");
Console.Write(e.ToString());
return;
}
//这里请求的相对地址,如果访问直接www.baidu.com则不需要,可以为空.
StringBuilder buf = new StringBuilder();
buf.Append("GET ").Append(path).Append(" HTTP/1.0/r/n");
buf.Append("Content-Type: application/x-www-form-urlencoded/r/n");
buf.Append("/r/n");
byte[] ms = System.Text.UTF8Encoding.UTF8.GetBytes(buf.ToString());
//发送
socket.Send(ms);
int recv =0; do
{
recv = socket.Receive(data);
//如果请求的页面meta中指定了页面的encoding为gb2312则需要使用对应的Encoding来对字节进行转换
//list.Text += (Encoding.UTF8.GetString(data, 0, recv));
list.Text += (Encoding.Default.GetString(data, 0, recv));
} while (recv != 0);
//禁用上次的发送和接受
socket.Shutdown( SocketShutdown.Both );
socket.Close();
}
}
}

C#用SOCKET发送HTTP请求小例的更多相关文章

  1. c/c++ socket发送http请求访问网站

    这几天课比较少,校园网上网要认证才能上网,每次必须输入学号密码,为了方便,写了一个自动登录以及如果在线,登录自服务系统强制下线的小工具. 强制下线思路:获取sessionID----------> ...

  2. 使用socket发送http请求(get/post)

    手动发送http请求 解释说明 https://blog.csdn.net/zhangliang_571/article/details/23508953 http://www.cnblogs.com ...

  3. PHP + Socket 发送http请求进而实现站点灌水

    本质上实现组装http信息的请求行,头信息.主题信息.參考it自学网 cookie信息和http请求头有非常大关系,注意把http请求头信息传递到函数里面 01-msg.php <?php re ...

  4. 【C语言】Socket发送HTTP-TCP请求,数据有字符串插入

    问题描述: 场景:编写Socket接口,向LOKI发送POST请求查询数据 BUG发现位置:通过cJSON读取时间戳,发现被截断. 现象:通过read()去读取返回的数据,数据行中被插入字符:如下 c ...

  5. linux c 使用socket 发送http请求 可以发送json格式数据

    #include <stdio.h>#include <sys/socket.h>#include <sys/types.h>#include <time.h ...

  6. php socket 发送http请求 GET POST

    http://docs.php-http.org/en/latest/httplug/users.html <?php /** * Created by PhpStorm. * User: Mc ...

  7. perl6 Socket: 发送HTTP请求

    sub MAIN(Str $host,Str $path, Int $port) { my $send = "GET $path HTTP/1.1\r\nHost: $host\r\n\r\ ...

  8. php socket 发送HTTP请求 POST json

    * HttpRequest.php <?php namespace et\http; /** * Created by PhpStorm. * User: mingzhanghui * Date ...

  9. socket发送http请求

随机推荐

  1. Caffeine缓存的简单介绍

    1.简介 在本文中,我们将了解Caffeine,一个用于Java的高性能缓存库. 缓存和Map之间的一个根本区别是缓存会清理存储的项目. 一个清理策略会决定在某个给定时间哪些对象应该被删除,这个策略直 ...

  2. vscode搭建python环境

    这两天刚下了一个pycharm,结果使用之后将vscode给崩了,重装的时候有些步骤也记不清,走了一些弯路,做个总结来记录一下(本人觉得vscode比pycharm好用一点). Python下载安装 ...

  3. C语言经典试题--指针

    分享一道C语言的经典的题目.题目要求如下: 利用字符指针实现字符串1"I Love China"与字符串2"So do I"的输出.然后利用字符指针将字符串2的 ...

  4. 高校表白App-团队冲刺第九天

    今天要做什么 在Fragment首页加上轮转播报,点击图片进入相应连接 做了什么 功能实现,通过连接第三方库来进行实现,比较简单.(url就可以) 遇到的问题 在调用以前的工具类时,有点小问题,发现以 ...

  5. Splay与FHQ-Treap

    两个一起学的,就放一块了. 主要是用来存板子. Splay //This is a Splay Tree. #include <cstdio> #include <cstring&g ...

  6. HashSet 和 HashMap 的比较

    HashMap 和 HashSet 都是 collection 框架的一部分,它们让我们能够使用对象的集合.collection 框架有自己的接口和实现,主要分为 Set 接口,List 接口和 Qu ...

  7. mongodb使用场景及与mysql区别

    MySQL是关系型数据库. 优势: 在不同的引擎上有不同 的存储方式. 查询语句是使用传统的sql语句,拥有较为成熟的体系,成熟度很高. 开源数据库的份额在不断增加,mysql的份额页在持续增长. 缺 ...

  8. vue实现单点登录的N种方式

    最近项目停工了,RageFrame的学习暂时告一段落,这一篇给大家分享下有关单点登录的相关知识,并提供一些demo给大家参考,希望对想了解的朋友有一些帮助. 话不多说,先上原理(借鉴地址:https: ...

  9. sql注入之堆叠注入及waf绕过注入

    #堆叠查询注入 1.堆叠查询概念 stacked injections(堆叠查询注入)从名词的含义就可以看出一应该是一堆(多条)sql语句一起执行.而在真实运用中也是如此,我们知道在mysql中,主要 ...

  10. Adaptive AUTOSAR 学习笔记 12 - 通信管理

    本系列学习笔记基于 AUTOSAR Adaptive Platform 官方文档 R20-11 版本 AUTOSAR_EXP_PlatformDesign.pdf 缩写 CM:Communicatio ...