现在的位置: 首页 > 综合 > 正文

C#用SOCKET发送HTTP请求小例

2013年08月19日 ⁄ 综合 ⁄ 共 2485字 ⁄ 字号 评论关闭
  1.    
  2. 调用处代码
  3.      private void button1_Click(object sender, EventArgs e)
  4.         {
  5.             string urlStr = this.textUrl.Text ;
  6.             if (urlStr == null || "".Equals(urlStr))
  7.             {
  8.                 MessageBox.Show( "必须填写要防问的地址!");
  9.                 return;
  10.             }
  11.             int port = 80;
  12.             try
  13.             {
  14.                 port = Convert.ToInt32(this.textPort.Text);
  15.             }
  16.             catch
  17.             {
  18.                 MessageBox.Show( "请填写正确的端口");
  19.             }
  20.             //可以为空
  21.             string path = "/"+this.textFile.Text;
  22.             this.textBox1.Text = "";
  23.             this.conn.StartAccess(urlStr, port, path, this.textBox1);
  24.         }

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Windows.Forms;
  8. namespace WindowsFormsApplication3
  9. {
  10.     class MyConnection
  11.     {
  12.         public void StartAccess( string urlStr ,int port , string path , TextBox list )
  13.         {
  14.             byte[] data = new byte[1024];
  15.             IPHostEntry gist = Dns.GetHostByName( urlStr );
  16.             //得到所访问的网址的IP地址
  17.             IPAddress ip = gist.AddressList[0];
  18.             IPEndPoint ipEnd = new IPEndPoint(ip, port);
  19.             //使用tcp协议 stream类型 (IPV4)
  20.             Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  21.             try
  22.             {
  23.                 socket.Connect(ipEnd);
  24.             }
  25.             catch (SocketException e)
  26.             {
  27.                 MessageBox.Show("与访问的服务器连接异常!");
  28.                 Console.Write(e.ToString());
  29.                 return;
  30.             }
  31.             //这里请求的相对地址,如果访问直接www.baidu.com则不需要,可以为空.
  32.             StringBuilder buf = new StringBuilder();
  33.             buf.Append("GET ").Append(path).Append(" HTTP/1.0/r/n");
  34.             buf.Append("Content-Type: application/x-www-form-urlencoded/r/n");
  35.             buf.Append("/r/n");
  36.             byte[] ms = System.Text.UTF8Encoding.UTF8.GetBytes(buf.ToString());
  37.             //发送
  38.             socket.Send(ms);
  39.             int recv =0;
  40.            
  41.             do
  42.             {
  43.                 recv = socket.Receive(data);
  44.                 //如果请求的页面meta中指定了页面的encoding为gb2312则需要使用对应的Encoding来对字节进行转换
  45.                 //list.Text += (Encoding.UTF8.GetString(data, 0, recv));
  46.                 list.Text += (Encoding.Default.GetString(data, 0, recv));
  47.             } while (recv != 0);
  48.             //禁用上次的发送和接受
  49.             socket.Shutdown( SocketShutdown.Both );
  50.             socket.Close();
  51.         }
  52.     }
  53. }

抱歉!评论已关闭.