[英]Problems POSTing data to an asp.net webpage (problem in Desktop C# end)
非常感謝您花時間閱讀我的帖子! 我在將數據從C#Desktop應用程序發布到C#asp.net網頁時遇到問題。 我相信問題在於桌面應用程序(或者至少有一個問題!)我還會發布我正在使用的asp.net代碼。 如果asp.net不是你的專長,請不要擔心,我只是想知道那里是否還有明顯的東西。
我還必須創建一個asp.net網站,將數據發布到Windows窗體應用程序。 這很完美。
這是我正在使用的代碼。 下面討論了什么不起作用。 我對所有這些asp.net的東西都很糟糕,所以你能提供的任何幫助都會非常感激。
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() && result == DialogResult.Yes)
{
string test = "Test";
WebRequest request = WebRequest.Create("http://localhost/test.aspx");
byte[] byteArray = Encoding.UTF8.GetBytes(test);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
// Give the response
using (Stream datastream = request.GetRequestStream())
{
datastream.Write(byteArray, 0, byteArray.Length);
}
}
但是,當我調試應用程序,並在datastream.Write()之后放置一個斷點時,我在Variable Watch窗口中出現了一些錯誤。 除了在那里,我沒有任何例外。
我似乎無法將圖像上傳到這個網站,所以我將它上傳到FreeWebs網站 - 抱歉,真的很尷尬! watch.jpg
如您所見,我在datastream.Length和datastream.Position上獲得System.NotSupported
你能幫我解決這個問題嗎? 謝謝!
為了防止asp.net程序員也看到這個,這個接收代碼有什么問題嗎?:
protected void Page_Load(object sender, EventArgs e)
{
string test = Request.BinaryRead(Request.TotalBytes).ToString();
}
謝謝大家,非常感謝你的時間!
理查德
編輯:關於gandjustas的評論,我提供更多信息。
鏈中的東西不起作用。 我沒有得到任何正式的例外報道。
如果我在asp.net網頁中使用此代碼:
string test = Request.BinaryRead(Request.TotalBytes).ToString();
Response.Clear();
Response.Write(test);
Response.End();
我收到以下回復:System.Byte []
這不是變量,而是包含任意單詞和符號'System.Byte []'的字符串
有些東西不起作用(顯然)我在Watch窗口中看到了這個System.NotSupportedException。 這讓我覺得有兩個錯誤:這個System.NotSupportedException需要在我的C#桌面應用程序中修復,而我的asp.net網頁在我從應用程序發送POST之前不應該顯示System.Byte []。
我需要幫助。 謝謝!
關於您的代碼的幾點評論:
application/x-www-form-urlencoded
內容類型,但您正在發送一些任意字節數組。 設置此內容類型時,服務器將期望使用它對請求進行編碼。 NotSupportedException
是正常的。 您根本無法在NetworkStream
上使用Length
屬性。 如果您真的想使用application/x-www-form-urlencoded
請嘗試簡化您的代碼:
客戶:
using (var client = new WebClient())
{
var values = new NameValueCollection
{
{ "key1", "value1" },
{ "key2", "value2" },
};
byte[] result = client.UploadValues("http://example.com/test.aspx", values);
}
服務器:
protected void Page_Load(object sender, EventArgs e)
{
string key1 = Request["key1"];
string key2 = Request["key2"];
}
嘗試這個
string test = "Test";
WebRequest request = WebRequest.Create("http://localhost/test.aspx");
request.Method = "POST";
request.ContentType = "text/xml;charset=utf-8";
request.ContentLength = test.Length;
using (StreamWriter paramWriter = new StreamWriter(request.GetRequestStream()))
{
paramWriter.Write(test, 0, test.Length);
}
WebResponse wres = request.GetResponse();
StreamReader sr = new StreamReader(wres.GetResponseStream());
string outdata = sr.ReadToEnd().Trim();
您似乎在WebRequest類上使用MSDN的HowTo ,這是正確的嗎?
嘗試使用NameValueCollection
,就像Darin所說的那樣,而不是使用字節數組,如下所示:
using (var client = new WebClient())
{
var values = new NameValueCollection
{
{ "key1", "value1" },
{ "key2", Convert.ToBase64String(File.ReadAllBytes(test)) },
};
byte[] result = client.UploadValues("http://example.com/test.aspx", values);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.