c#将值从asp.net网站传递到windows窗体
本文关键字:windows 窗体 网站 net asp | 更新日期: 2023-09-27 18:27:59
我制作了一个网站和一个windows窗体应用程序,它们都连接到同一个MySQL数据库。windows应用程序通过参数(用户名等)向网站发出POST请求。服务器(网站)生成一对RSA公钥和私钥。私钥存储在数据库中,公钥发送到客户端(windows应用程序)。问题是,当我尝试发送公钥时,它没有被发送。它发送其他信息,如网页的html代码和公钥,我在加密时收到错误"无效语法"(显然是因为发送了html代码和私钥)。我如何只提取公钥并只将其发送给客户端。但我得到了这样的输出:http://postimg.org/image/8kwpnl6lp/
我做错了什么。我怎么只能从服务器获得公钥作为响应。感谢的帮助
这是我的代码:
客户端(windows应用程序:向服务器发出发布请求):
public partial class Form1 : Form
{
string Url = "http://localhost:8731/ckey.aspx"; //creates public key
string userName = Textbox1.TExt;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string publicKey = getPublicKey(Url);
richTextBox1.Text =publicKey ;
}
public string getPublicKey(string url)
{
WebClient webClient = new WebClient();
NameValueCollection formData = new NameValueCollection();
formData["username"] = userName;
byte[] responseBytes = webClient.UploadValues(url, "POST", formData);
string responsefromserver = Encoding.UTF8.GetString(responseBytes);
webClient.Dispose();
return responsefromserver;
}
}
以及服务器端代码(生成rsa密钥对并向客户端[ckey.aspx]发送公钥)
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string response = AssignNewKey();
Response.Write(response);
}
public string AssignNewKey()
{
string username1 =Request.Form["username"];
RSA rsa1 = new RSACryptoServiceProvider(2048);
string publicPrivateKeyXML = rsa1.ToXmlString(true);
string publicOnlyKeyXML = rsa1.ToXmlString(false);
using (MySqlConnection myConn = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString))
{
string query = "INSERT INTO securelogin (PrivateKey,username)";
query += " VALUES (@PrivateKey,@username)";
MySqlCommand myCommand = new MySqlCommand(query, myConn);
myCommand.Parameters.AddWithValue("@PrivateKey", publicPrivateKeyXML);
myCommand.Parameters.AddWithValue("@username", username1);
myConn.Open();
myCommand.ExecuteNonQuery();
myConn.Close();
}
return publicOnlyKeyXML;
}
如果通过Response.Write
传递响应,则应考虑添加Response.End/Close
。与我们对send/download
文件所做的相同
否则,它将呈现页面并添加页面HTML。
您应该使用类似WebService(.asmx)的东西
阅读本教程:http://www.tutorialspoint.com/asp.net/asp.net_web_services.htm