C#用SOCKET发送HTTP请求小例

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请求小例的更多相关文章
- c/c++ socket发送http请求访问网站
这几天课比较少,校园网上网要认证才能上网,每次必须输入学号密码,为了方便,写了一个自动登录以及如果在线,登录自服务系统强制下线的小工具. 强制下线思路:获取sessionID----------> ...
- 使用socket发送http请求(get/post)
手动发送http请求 解释说明 https://blog.csdn.net/zhangliang_571/article/details/23508953 http://www.cnblogs.com ...
- PHP + Socket 发送http请求进而实现站点灌水
本质上实现组装http信息的请求行,头信息.主题信息.參考it自学网 cookie信息和http请求头有非常大关系,注意把http请求头信息传递到函数里面 01-msg.php <?php re ...
- 【C语言】Socket发送HTTP-TCP请求,数据有字符串插入
问题描述: 场景:编写Socket接口,向LOKI发送POST请求查询数据 BUG发现位置:通过cJSON读取时间戳,发现被截断. 现象:通过read()去读取返回的数据,数据行中被插入字符:如下 c ...
- linux c 使用socket 发送http请求 可以发送json格式数据
#include <stdio.h>#include <sys/socket.h>#include <sys/types.h>#include <time.h ...
- php socket 发送http请求 GET POST
http://docs.php-http.org/en/latest/httplug/users.html <?php /** * Created by PhpStorm. * User: Mc ...
- perl6 Socket: 发送HTTP请求
sub MAIN(Str $host,Str $path, Int $port) { my $send = "GET $path HTTP/1.1\r\nHost: $host\r\n\r\ ...
- php socket 发送HTTP请求 POST json
* HttpRequest.php <?php namespace et\http; /** * Created by PhpStorm. * User: mingzhanghui * Date ...
- socket发送http请求
随机推荐
- Linux下如何使用Rsync备份服务器重要数据
Rsync介绍: Rsync英文全称Remote synchronization,从软件的名称就可以看出来,Rsync具有可使本地和远程两台主机之间的数据快速复制同步镜像,远程备份的功能,这个功能类似 ...
- Java003-String字符串
1.这两种定义有什么区别 /*** * 面试题:这两种定义方式有什么区别? * 如何证明? * @param args */ public static void main(String[] args ...
- CLion安装、激活、配置教程
clion下载 1.进入官网 https://www.jetbrains.com/zh-cn/clion/download/#section=windows下载,下载.exe文件即可 2.点击下载好的 ...
- Ubuntu20.4 bs4安装的正确姿势
一.背景 公司一小伙子反馈在内网机器上通过代理,还是安装不了bs4:于是乎,作为菜鸡的我开始排查.一直认为是网络和代理问题,所以关注点一直放在网络和安装包上:在网上搜索到,主要是以下问题: 1)更新a ...
- 常见数据库SELECT结果只显示前几条记录方法汇总
常见数据库SELECT结果只显示前几条记录方法汇总 为了查看数据表中的数据情况.经常会遇到想让查询结果只显示N行,比如只显示10行的情况.不同的数据库有不同的关键字和SELECT实现语法. 1.SQL ...
- JAVA基础之JDK、JRE、JVM关系
什么是JRE和JDK JDK(Java Development Kit Java开发工具包) JDK是提供给Java开发人员使用的,其中包含了java的开发工具,也包括了JRE.所以安装了JDK,就不 ...
- [刘阳Java]_easyui-panel组件入门级_第3讲
EasyUI中的panel组件在前面一节中我们简单告诉了大家代码如何写.这一节我们会从panel的入门级开始讲起走,重点包括它的事件监听,属性tool介绍 1. 事件监听-通过data-options ...
- Ubuntu 18.04 开启 root 账号并允许远程连接
转载:https://blog.csdn.net/u010766726/article/details/105376461 以普通用户登录系统 通过 "终端" 操作 普通用户 – ...
- 两万字Vue.js基础学习笔记(二)
Vue.js学习笔记(二) 4.模块化开发 ES6模块化的导入和导出 我们使用export指令导出了模块对外提供的接口,下面我们就可以通过import命令来加载对应的这个模块了 首先,我们需要在HTM ...
- C语言学习之基本数据类型【一】
近期学习鸿蒙硬件物联网开发,用到的开发语言是C: 一.基础语法:第一个案例: 命令 gcc hello.c #include <stdio.h> //stdio.h 是一个头文件 , #i ...