[英]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.