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请求
随机推荐
- 利用漏洞破解win7密码
一.利用5次shift漏洞破解win7密码 1.在未登录系统时,连续按5次shift键,弹出程序:C:\Windows\system32\sethc.exe 2.部分win7及win10可通过系统修复 ...
- PYTHON 得到ADB的输出结果
#利用ADB DEVICES结果判断指定手机是否正常连接,如果为offline,则adb disconnect sjh:adb connect sjh#如果没有,则执行adb connect sjhd ...
- c语言字符串存储方式
#include <stdio.h> // C 语言中,任何数据类型都不可以直接存储一个字符串.那么字符串如何存储? //在 C 语言中,字符串有两种存储方式,一种是通过字符数组存储,另一 ...
- [善用佳软]Chrome插件之彩云小译
最近公众号新推出个栏目[善用佳软],旨在和大家分享各种小插件和制作开源小软件. 最近在浏览外网时,发现一个贼好用的翻译神器---[彩云小译],在网页端就可以快速的对照翻译出来,经过试用发现无论是速度还 ...
- PAT甲级:1136 A Delayed Palindrome (20分)
PAT甲级:1136 A Delayed Palindrome (20分) 题干 Look-and-say sequence is a sequence of integers as the foll ...
- 光学动作捕捉系统中的反光标识点(Marker点)
动作捕捉系统本质上是一种定位系统,通常需要在目标物布置定位设备进行追踪.以红外光学为原理的动作捕捉系统,主要由由光学镜头.动作捕捉软件.反光标识点.POE交换机.和若干配件组成,其中反光标识点(Mar ...
- informix数据库分页
需求描述 当查询结果返回大量数据情况下,比如报表查询.需要按一定条件排序提供分页呈现数据. INFORMIX实现方案:Informix 数据库提供了非常便捷.高效的SQL. SELECT SKIP M ...
- 公有组件ShowCodeList实现原理之一一下拉框的实现
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- xampp搭建开源项目iwebshop后,服务器重启后再启动xampp显示组件都启动ok,但是实际启动失败解决办法
最近用xampp搭建了开源商城项目iwebshop,刚搭建完没问题,可是周一来因为服务器重启了,我再启动xampp,显示组件都启动ok了,但是用linux命令查看相关组件的进程时,发现实际没启动起来, ...
- js学习笔记之字符串统计出现次数最多的字符
1.方法:把字符串中字符替换为空,并和之前的字符串的长度相减,得到已经被替换的字符的数量,依次比较获得出现次数最多的字符 var str ="adadfdfseffserfefsefseef ...