利用ASP.NET WebClient的UploadData來做一個C#與VB.Net Code Convert的小程式
由上篇文章"利用ASP.NET WebClient的UploadData來做一個自動登入網頁的小程式"
小弟再寫一個簡單的應用...C#與VB.Net程式碼相互轉換的小程式......
ps.主要轉換的程式碼是由 http://labs.developerfusion.co.uk/convert/vb-to-csharp.aspx 提供
c#範例
PostData.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PostData.aspx.cs" Inherits="PostData" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>PostData</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Height="200px" TextMode="MultiLine" Width="450px"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Convert to C#" Width="150px" /><br />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Convert to VB.Net"
Width="150px" /></div>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>PostData</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Height="200px" TextMode="MultiLine" Width="450px"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Convert to C#" Width="150px" /><br />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Convert to VB.Net"
Width="150px" /></div>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</form>
</body>
</html>
PostData.aspx.cs






























WebClient webClient = new WebClient();

webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

byte[] postData = Encoding.Default.GetBytes(postString);

byte[] responseData = webClient.UploadData(uriString, "POST", postData);

string srcString = Encoding.Default.GetString(responseData);

string start = "<p>Here's your C# code, ready to go!</p>";

string end = "<h3>Want more code converted?</h3>";

int sValue = srcString.IndexOf(start) + start.Length;

int eValue = srcString.IndexOf(end);

this.Literal1.Text = srcString.Substring(sValue,eValue-sValue);












WebClient webClient = new WebClient();

webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

byte[] postData = Encoding.Default.GetBytes(postString);

byte[] responseData = webClient.UploadData(uriString, "POST", postData);

string srcString = Encoding.Default.GetString(responseData);

string start = "<p>Here's your VB.NET code, ready to go!</p>";

string end = "<h3>Want more code converted?</h3>";

int sValue = srcString.IndexOf(start) + start.Length;

int eValue = srcString.IndexOf(end);

this.Literal1.Text = srcString.Substring(sValue, eValue - sValue);


執行結果:
參考網址:http://www.dotblogs.com.tw/puma/archive/2008/03/24/2035.aspx