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. C语言:九宫格

    #include <stdio.h> /* 如下排列表示 A00 A01 A02 A10 A11 A12 A20 A21 A22 */ int main() { unsigned char ...

  2. python 读写sql2008 类

    import pymssql class MSSQL: def __init__(self,host,user,pwd,db): self.host = host self.user = user s ...

  3. 谷粒学院-2-mybatisplus

    一.参考文档 官网:http://mp.baomidou.com/ 参考教程:http://mp.baomidou.com/guide/ MyBatis-Plus(简称 MP)是一个 MyBatis ...

  4. 微信小程序云开发-数据库-查询满足条件的数据

    一.查询价格大于10的商品 1.wxml文件 2.js文件 where条件语句:.where({price:db.command.gt(10)}) 3.查询结果 二.查询价格大于等于10的商品 js文 ...

  5. GraphQL 概念入门

    GraphQL 概念入门 Restful is Great! But GraphQL is Better. -- My Humble Opinion. GraphQL will do to REST ...

  6. SpringBoot | 3.1 配置数据源

    目录 前言 1. 数据源的自动配置 2. *数据源自动配置源码分析 2.1 DataSourceAutoConfiguration:数据源自动配置类 2.2 JdbcTemplateAutoConfi ...

  7. Leetcode:面试题 04.04. 检查平衡性

    Leetcode:面试题 04.04. 检查平衡性 Leetcode:面试题 04.04. 检查平衡性 Talk is cheap . Show me the code . /** * Definit ...

  8. SQL SERVER 雨量计累计雨量(小时)的统计思路

    PLC中定时读取5分钟雨量值,如何将该值统计为小时雨量作为累计?在sql server group by聚合函数,轻松实现该目的. 1.编写思路 数据库中字段依据datetime每五分钟插入一条语句, ...

  9. markdown文档编写基础

    Markdown快速入门教程 ###########来源:https://zhuanlan.zhihu.com/p/84918488 ###########来源:https://github.com/ ...

  10. 记录21.08.04 — mybatis入门学习

    mybatis入门 mybatis简介 MyBatis 是一款优秀的持久层框架,它支持自定义 SQL.存储过程以及高级映射.MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工 ...