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

一、VB.NET Code

 

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

 

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();

	}
}

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值