Gửi và nhận dữ liệu giữa phần mềm Windows Form và Website bằng C#

Trong lập trình, đôi khi cần thiết phải thực hiện yêu cầu tương tác giữa phần mềm ứng dụng và webiste, chẳng hạn như việc kiểm tra phiên bản mới (phần mềm sẽ gửi thông tin yêu cầu lên website theo định kỳ hoặc lúc khởi động và website sẽ trả về thông tin là có phiên bản mới hay chưa) hoặc kiểm tra license key khi khách hàng mua phần mềm (khi cài đặt xong, phần mềm sẽ gửi  thông tin lên webserver của nhà sản xuất phần mềm và license key sẽ được kiểm tra tại server),vv…

Để tạo ứng dụng gửi và nhân dữ liệu từ phần mềm và website, thực hiện viết code ở 2 phía: trên phần mềm và website. Bài viết này sử dụng website viết bằng PHP.

Code ở phía Windows Form:

Đầu tiên tạo 1 project mới trên Visual studio, ngoài các thư viện mặc định, cần bổ sung một số thư viện với khai báo using như sau:

using System;
using System.Text;
using System.Net;
using System.IO;

Viết hàm xử lý gửi dữ liệu lên website:

public string do_request()
{
string uri="http://localhost/MyWeb/test.php"; // địa chỉ trang web mà phần mềm gửi thông tin.
string SoftName="mysoft"; // tên của chương trình cần kiểm tra phiên bản.
string Version=6.0; // phiên bản hiện tại của phần mềm
       string respondFromWeb=""; //Biến lưu dữ liệu phản hồi từ website
byte[] bytesDataSend; //Biến lưu dữ liệu send ở dạng byte      
       string sendData = "SoftName=" + SoftName + "&Version=" + Version; //Biến lưu chuỗi dữ liệu mà phần mềm sẽ gửi lên webiste. Ví dụ như gửi thông tin tên phần mềm và phiên bản để check phiên bản mới. Lưu ý, giữa 2 giá trị gửi đi phải có ký tự & ở giữa.
       WebRequest request = WebRequest.Create(uri);
       request.Method = "POST";
       request.Credentials = CredentialCache.DefaultCredentials;
       System.Net.ServicePointManager.Expect100Continue = false;
       bytesDataSend = Encoding.UTF8.GetBytes(sendData);
       request.ContentLength = bytesDataSend.Length;
       request.ContentType = "application/x-www-form-urlencoded";
       Stream stream = request.GetRequestStream();
       stream.Write(bytesDataSend, 0, bytesDataSend.Length);
       stream.Close();
       HttpWebResponse response = (HttpWebResponse)request.GetResponse();
       Stream streamRes = response.GetResponseStream();
       StreamReader reader = new StreamReader(streamRes);
       respondFromWeb = reader.ReadToEnd();
       reader.Close();
       streamRes.Close();
       response.Close();
       return respondFromWeb;
  }

Code phía Website PHP (địa chỉ webpage giống giá trị chuỗi uri như trên: http://localhost/MyWeb/test.php)

Code trong file test.php

<?php
	include 'config.php';
	$SoftName=$_POST['SoftName'];
    $Version=$_POST['Version'];
    $NewVersion=7.0;
	if($SoftName=="mysoft" && $Version<$NewVersion)
    {
        $response="Co version moi";
    }
    else
    {
        $response="Chua có version moi";
    }
echo $response;
?>

Code trong file config.php

<?php
DEFINE('DB_USER','myuser');//myuser là user database mysql
	DEFINE('DB_PASSWORD','123456');//password đăng nhập database mysql
	DEFINE('DB_HOST','localhost');//host name của website hoặc localhost
	DEFINE('DB_NAME','mydb');//tên cơ sơ dữ liệu trên mysql
	$conn = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);  	 
    if (!$conn) 
  	{
  		die("Connection failed: " . mysqli_connect_error());
  	}
?>

Related posts

Leave a Comment