本文介绍了Windows应用程序从Web服务器获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨朋友们, 我有一个需要从网络服务器检索一些信息的Windows应用程序。 并且,SQL Server不对公众开放。所以我创建了一个ASP.NET页面,它读取数据并将数据作为XML发回。我将在我的应用程序中使用它并处理它。 这是正确的方式吗?或者我应该使用任何其他技术? 这是我的ASPNet代码,它以XML格式发送数据...Hi friends,I have a windows application that needs to retrieve some information from the webserver.And, the SQL Server is not open to the Public. So I created one ASP.NET page that reads the data and sends back the data as XML. I will use this in my application and process it.Is this the right kind of way? or should I use any other technologies ??This is my ASPNet Code that sends the data as XML...protected void Page_Load(object sender, EventArgs e) { try { Response.ContentType = "text/plain"; string u_id_str = (Request["userid"] != null ? Request["userid"] : ""), hdd_code = (Request["hdcode"] != null ? Request["hdcode"] : ""); if (u_id_str != "") { int u_id = -1; if (Int32.TryParse(u_id_str, out u_id)) { SqlConnection con = new SqlConnection("Data Source=SQLServerName;Initial Catalog=DB;User Id=Username;Password=Password"); SqlCommand com = new SqlCommand(); com.Connection = con; con.Open(); com.CommandText = "SELECT UserID FROM vw_Users WHERE UserId = " + u_id; SqlDataReader dread = com.ExecuteReader(); if (dread.Read()) { if (!dread.IsClosed) dread.Close(); com.CommandText = "SELECT * FROM fconvwpurchased WHERE customerid = " + u_id + " AND expirydate > '" + DateTime.Today.ToString("dd/MMM/yyyy") + "'"; DataSet DsPurchased = new DataSet("LicenseList"); DataTable DtPurchased = new DataTable("fconvwpurchased"); DsPurchased.Tables.Add(DtPurchased); SqlDataAdapter sdad = new SqlDataAdapter(com); sdad.Fill(DtPurchased); if (DtPurchased.Rows.Count > 0 ) { foreach (DataRow drr in DtPurchased.Rows) { com.CommandText = "SELECT COUNT(sl) FROM fcontblLicenses WHERE productcode = '" + drr["ProCode"].ToString() + "' AND customerid = " + u_id; if (com.ExecuteScalar() != null && com.ExecuteScalar() != DBNull.Value) { drr["qty"] = Convert.ToInt32(drr["qty"]) - Convert.ToInt32(com.ExecuteScalar()); DtPurchased.AcceptChanges(); } } StringWriter sw = new StringWriter(); DsPurchased.WriteXml(sw); Response.Write(sw.ToString()); } else { Response.Write("NO_DATA"); } } else Response.Write("NO_USER"); if (!dread.IsClosed) dread.Close(); con.Close(); } else Response.Write("NO_USER"); } else Response.Write("NO_USER"); } catch { Response.Write("ERROR"); } } 和我的Windows应用程序,我正在使用这个...and for my Windows Application, I am using this...private string HttpRequestServer(string urltoconnect, string postData) { try { // Create a request using a URL that can receive a post. WebRequest request = WebRequest.Create(urltoconnect); // Set the Method property of the request to POST. request.Method = "POST"; // Create POST data and convert it to a byte array. byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Set the ContentType property of the WebRequest. request.ContentType = "application/x-www-form-urlencoded"; // Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length; // Get the request stream. Stream dataStream = request.GetRequestStream(); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); // Close the Stream object. dataStream.Close(); // Get the response. WebResponse response = request.GetResponse(); // Display the status. Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); // Display the content. // Clean up the streams. reader.Close(); dataStream.Close(); response.Close(); return responseFromServer; } catch { return "ERROR"; } }推荐答案 这篇关于Windows应用程序从Web服务器获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-07 05:06