.NET实现应用程序登录Web页

一、VB.NET Code

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Imports System.Text
Imports System.Net
Imports System.IO
 
Public Class UwantsLogin
    Private loginID As String = "spang"
    Private loginPass As String = "123456"
 
    Public Sub New()
        Dim encoding As New UTF8Encoding
        Dim postData As String = "username=" & loginID & "&password=" & loginPass
 
        Dim data() As Byte = encoding.GetBytes(postData)
 
        'Prepare web request
        Dim request As HttpWebRequest = CType(WebRequest.Create("http://www5.uwants.com/logging.php?action=login"), HttpWebRequest)
        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = data.Length
        Dim newStream As Stream = request.GetRequestStream()
 
        'Send the data
        newStream.Write(data, 0, data.Length)
        newStream.Close()
 
        'Get response
        Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        Dim reader As StreamReader = New StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("big5"))
        Dim content As String = reader.ReadToEnd()
 
    End Sub
 
End Class

二、Visual C# Code

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Text;
using System.Net;
using System.IO;
 
public class UwantsLogin
{
    private string loginID = "spang";
 
    private string loginPass = "123456";
    public UwantsLogin()
    {
        UTF8Encoding encoding = new UTF8Encoding();
        string postData = "username=" + loginID + "&password=" + loginPass;
 
        byte[] data = encoding.GetBytes(postData);
 
        //Prepare web request
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www5.uwants.com/logging.php?action=login");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = data.Length;
        Stream newStream = request.GetRequestStream();
 
        //Send the data
        newStream.Write(data, 0, data.Length);
        newStream.Close();
 
        //Get response
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("big5"));
        string content = reader.ReadToEnd();
 
    }
}

 

 

 

posted @   Sunny Peng  阅读(987)  评论(1编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
点击右上角即可分享
微信分享提示