大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

vb.net网站交互,VBNET教程

VB.NET 服务器与客户端数据交互问题?

客户端简单代码:

创新互联建站主要从事成都网站制作、做网站、外贸营销网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务枝江,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220

Imports System.Net.Sockets

Imports System.IO

Private Output As NetworkStream

Private Writer As BinaryWriter

Private Reader As BinaryReader

Dim StrSend As String

Dim client As New TcpClient '

client.Connect(servIP, 2006)

Output = client.GetStream

Writer = New BinaryWriter(Output)

Reader = New BinaryReader(Output)

StrSend = "字符串"

System.Threading.Thread.Sleep(100)

Writer.Write(StrSend)

Try

Message = Reader.ReadString

If Message = "OK" Then

LBinfo.Text = "成功!" '测试

Else

LBinfo.Text = "失败!"

End If

System.Threading.Thread.Sleep(100)

Catch ex As Exception

MessageBox.Show("Client Application Closing!")

Finally

Writer.Close()

Reader.Close()

Output.Close()

client.Close()

End Try

服务器端

用线程

Imports System.Net.Sockets

Imports System.IO

Imports System.Threading

Imports System.Windows.Forms

Private Connection As Socket

Private readThread As Thread

Private SocketStream As NetworkStream

Private Writer As BinaryWriter

Private Reader As BinaryReader

Public Sub New()

MyBase.New()

'该调用是 Windows 窗体设计器所必需的。

InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

readThread = New Thread(AddressOf RunServer)

readThread.Start()

End Sub

Public Sub RunServer()

Dim Listener As TcpListener

Try

Listener = New TcpListener(Net.IPAddress.Parse("192.168.1.16"), 2006)

Listener.Start()

While True

Connection = Listener.AcceptSocket 'accept an incoming connection

SocketStream = New NetworkStream(Connection) 'create networkstream object associated with socket

Writer = New BinaryWriter(SocketStream) 'create object for transferring data across stream

Reader = New BinaryReader(SocketStream) 'read string data sent from client

Try

Do

strIncept = Reader.ReadString

strIncept = DataDisp(strIncept) '此处插入数据处理程序()

SendInfoToClient(strIncept) 'send to client

Loop While Connection.Connected

Catch ex As Exception

MsgBox(ex.ToString)

Finally

Writer.Close()

Reader.Close()

SocketStream.Close()

End Try

End While

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Sub

Public Sub SendInfoToClient(ByVal Info As String)

'Send to client

Try

If Connection.Connected Then

Writer.Write(Info)

Connection.Close()

End If

Catch ex As SocketException

MessageBox.Show(ex.ToString)

End Try

End Sub

整个过程都有了

VB.NET环境下webbrowser网页与Winform程序交互如何实现?

public Form1()

{

InitializeComponent();

webBrowser1.NewWindow += new CancelEventHandler(webBrowser1_NewWindow);

}

void webBrowser1_NewWindow(object sender, CancelEventArgs e)

{

e.Cancel=true;

webBrowser1.Navigate(webBrowser1.StatusText);

}

用vb.net实现网络通信

用 vb.net socket通信

Dim th As Threading.Thread

2 Dim tcpl As System.Net.Sockets.TcpListener

3

4 Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

5 th = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf MyListen))

6 th.Start()

7 End Sub

8

9 Public Sub SendMessage()Sub SendMessage(ByVal IP As String, ByVal SendMsg As String)

10 Try

11 If IP "" Then

12 Dim tcpc As New System.Net.Sockets.TcpClient(IP, 5656)

13 Dim tcpStream As Net.Sockets.NetworkStream = tcpc.GetStream

14 Dim reqStream As New IO.StreamWriter(tcpStream)

15 reqStream.Write(SendMsg)

16 reqStream.Flush()

17 tcpStream.Close()

18 tcpc.Close()

19 End If

20 Catch ex As Exception

21 MsgBox(ex.Message.ToString)

22 End Try

23 End Sub

24 Private Sub MyListen()Sub MyListen()

25 Try

26 Dim ipAddress As System.Net.IPAddress = System.Net.Dns.Resolve(System.Net.Dns.GetHostName).AddressList(0)

27 tcpl = New System.Net.Sockets.TcpListener(ipAddress, 5656)

28 tcpl.Start()

29 While True

30 Dim s As System.Net.Sockets.Socket = tcpl.AcceptSocket()

31 Dim MyBuffer(1024) As Byte

32 Dim i As Integer

33 i = s.Receive(MyBuffer)

34 If i 0 Then

35 Dim lstrRec As String

36 Dim j As Integer

37 For j = 0 To i - 1

38 TextBox1.Text += Chr(MyBuffer(j)) ","

39 Next

40 End If

41 End While

42 Catch ex As Exception

43 MsgBox(ex.Message.ToString)

44 End Try

45 End Sub

46

47 Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

48 SendMessage("192.168.0.61", TextBox2.Text)

49 End Sub

vb.net/.net怎样使用http协议的get和post?

不想一点一点写了,粘贴给你吧

使用 HttpWebRequest 向网站提交数据

HttpWebRequest 是 .net 基类库中的一个类,在命名空间 System.Net 下面,用来使用户通过 HTTP 协议和服务器交互。

HttpWebRequest 对 HTTP 协议进行了完整的封装,对 HTTP 协议中的 Header, Content, Cookie 都做了属性和方法的支持,很容易就能编写出一个模拟浏览器自动登录的程序。

程序使用 HTTP 协议和服务器交互主要是进行数据的提交,通常数据的提交是通过 GET 和 POST 两种方式来完成,下面对这两种方式进行一下说明:

1. GET 方式。 GET 方式通过在网络地址附加参数来完成数据的提交,比如在地址 中,前面部分 表示数据提交的网址,后面部分 hl=zh-CN 表示附加的参数,其中 hl 表示一个键(key), zh-CN 表示这个键对应的值(value)。程序代码如下:

HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "" );

req.Method = "GET";

using (WebResponse wr = req.GetResponse())

{

//在这里对接收到的页面内容进行处理

}

2. POST 方式。 POST 方式通过在页面内容中填写参数的方法来完成数据的提交,参数的格式和 GET 方式一样,是类似于 hl=zh-CNnewwindow=1 这样的结构。程序代码如下:

string param = "hl=zh-CNnewwindow=1";

byte[] bs = Encoding.ASCII.GetBytes(param);

HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "" );

req.Method = "POST";

req.ContentType = "application/x-www-form-urlencoded";

req.ContentLength = bs.Length;

using (Stream reqStream = req.GetRequestStream())

{

reqStream.Write(bs, 0, bs.Length);

}

using (WebResponse wr = req.GetResponse())

{

//在这里对接收到的页面内容进行处理

}

在上面的代码中,我们访问了 的网址,分别以 GET 和 POST 方式提交了数据,并接收了返回的页面内容。然而,如果提交的参数中含有中文,那么这样的处理是不够的,需要对其进行编码,让对方网站能够识别。

3. 使用 GET 方式提交中文数据。 GET 方式通过在网络地址中附加参数来完成数据提交,对于中文的编码,常用的有 gb2312 和 utf8 两种,用 gb2312 方式编码访问的程序代码如下:

Encoding myEncoding = Encoding.GetEncoding("gb2312");

string address = "?" + HttpUtility.UrlEncode("参数一", myEncoding) + "=" + HttpUtility.UrlEncode("值一", myEncoding);

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(address);

req.Method = "GET";

using (WebResponse wr = req.GetResponse())

{

//在这里对接收到的页面内容进行处理

}

在上面的程序代码中,我们以 GET 方式访问了网址 ,传递了参数“参数一=值一”,由于无法告知对方提交数据的编码类型,所以编码方式要以对方的网站为标准。常见的网站中, (百度)的编码方式是 gb2312, (谷歌)的编码方式是 utf8。

4. 使用 POST 方式提交中文数据。 POST 方式通过在页面内容中填写参数的方法来完成数据的提交,由于提交的参数中可以说明使用的编码方式,所以理论上能获得更大的兼容性。用 gb2312 方式编码访问的程序代码如下:

Encoding myEncoding = Encoding.GetEncoding("gb2312");

string param = HttpUtility.UrlEncode("参数一", myEncoding) + "=" + HttpUtility.UrlEncode("值一", myEncoding) + "" + HttpUtility.UrlEncode("参数二", myEncoding) + "=" + HttpUtility.UrlEncode("值二", myEncoding);

byte[] postBytes = Encoding.ASCII.GetBytes(param);

HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "" );

req.Method = "POST";

req.ContentType = "application/x-www-form-urlencoded;charset=gb2312";

req.ContentLength = postBytes.Length;

using (Stream reqStream = req.GetRequestStream())

{

reqStream.Write(bs, 0, bs.Length);

}

using (WebResponse wr = req.GetResponse())

{

//在这里对接收到的页面内容进行处理

}

从上面的代码可以看出, POST 中文数据的时候,先使用 UrlEncode 方法将中文字符转换为编码后的 ASCII 码,然后提交到服务器,提交的时候可以说明编码的方式,用来使对方服务器能够正确的解析。

以上列出了客户端程序使用 HTTP 协议与服务器交互的情况,常用的是 GET 和 POST 方式。现在流行的 WebService 也是通过 HTTP 协议来交互的,使用的是 POST 方法。与以上稍有所不同的是, WebService 提交的数据内容和接收到的数据内容都是使用了 XML 方式编码。所以, HttpWebRequest 也可以使用在调用 WebService 的情况下。


网站标题:vb.net网站交互,VBNET教程
文章URL:http://dzwzjz.com/article/dsieedd.html
在线咨询
服务热线
服务热线:028-86922220
TOP