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请求
随机推荐
- C语言:监听键盘
所谓键盘监听,就是用户按下某个键时系统做出相应的处理,本章讲到的输入输出函数也是键盘监听函数的一种,例如 getchar().getche().getch() 等.下面的代码演示了 getche() ...
- C语言:extern应用
前面我们都是将所有的代码写到一个源文件里面,对于小程序,代码不过几百行,这或许无可厚非,但当程序膨胀代码到几千行甚至上万行后,就应该考虑将代码分散到多个文件中,否则代码的阅读和维护将成为一件痛苦的事情 ...
- java 实现中英文拼写检查和错误纠正?可我只会写 CRUD 啊!
简单的需求 临近下班,小明忙完了今天的任务,正准备下班回家. 一条消息闪烁了起来. "最近发现公众号的拼写检查功能不错,帮助用户发现错别字,体验不错.给我们系统也做一个." 看着这 ...
- 如何监控 Log4j2 异步日志遇到写入瓶颈
如何监控 Log4j2 异步日志遇到写入瓶颈 在之前的一篇文章中(一次鞭辟入里的 Log4j2 异步日志输出阻塞问题的定位),我们详细分析了一个经典的 Log4j2 异步日志阻塞问题的定位,主要原因还 ...
- robotframework - selenium Api介绍
一.介绍下selenium常用的api *** Settings ***Library SeleniumLibraryResource baidu业务.txtResource UI分层.txt *** ...
- 秒懂 Java 的三种代理模式
前言 代理(Proxy)模式是一种结构型设计模式,提供了对目标对象另外的访问方式:即通过代理对象访问目标对象. 这样做的好处是:可以在目标对象实现的基础上,增强额外的功能操作,即扩展目标对象的功能. ...
- AntDesignBlazor 学习笔记
AntDesignBlazor是基于 Ant Design 的 Blazor 实现,开发和服务于企业级后台产品.我的 Blazor Server 学习就从这里开始,有问题可以随时上 Blazor 中文 ...
- 从redis数据库取数据存放到本地mysql数据库
redis数据库属于非关系型数据库,数据存放在内存堆栈中,效率比较高. 其存储数据是以json格式字符串存储字典的,而类似的关系型数据库无法实现这种数据的存储. 在爬取数据时,将数据暂存到redis中 ...
- Python基础之用PyQt5创建menu
前一篇文章中,我们已经安装了PyQt5,并且已经测试过可用.那么接下来第一步开始学习如何创建菜单. 第一步:在想要运行py的地方右击External Tools-->designer,打开des ...
- 基于pygame框架的打飞机小游戏
import pygame from pygame.locals import * import time import random class Base(object): "" ...