【发布时间】:2014-04-18 11:44:21
【问题描述】:
我正在使用 asp.net 编写一个 Web 应用程序。我通过一些不公开但对我的服务器端可用的网络连接到 JSP 页面。我的目标是从我的 asp.net 页面(将是公开的)将数据发布到 jsp 页面(对用户来说是私有的),然后检索 jsp.page 的答案
protected void Page_Load(object sender, EventArgs e)
{
Page page = HttpContext.Current.Handler as Page;
String Url = "https://192.168.0.1:1111/aaa/sss/test.jsp";
StringBuilder postData = new StringBuilder();
postData.Append("a=" + p.Request.Form[0] + "&");
postData.Append("b=" + p.Request.Form[1] + "&");
postData.Append("c=" + p.Request.Form[2] + "&");
postData.Append("d=" + p.Request.Form[3] + "&");
postData.Append("e=" + p.Request.Form[4] + "&");
postData.Append("f=" + p.Request.Form[5] + "&");
postData.Append("g=" + p.Request.Form[6] + "&");
postData.Append("h=" + p.Request.Form[7]);
StreamWriter writer = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "text/html";
request.ContentLength = postData.ToString().Length;
try
{
writer = new StreamWriter(request.GetRequestStream());
writer.Write(postData.ToString());
System.Web.HttpContext.Current.Response.Write(writer.ToString());
}
finally
{
if (writer != null)
writer.Close();
}
}
【问题讨论】:
-
你试过什么?将此分为两个问题:1)您的 ASP.NET 应用程序接受来自用户的输入。 2) 您的服务器端代码向外部资源发送请求。两者是分开的。您可以在 .NET 中使用
HttpClient之类的东西向外部资源发送请求并读取响应。 -
#Luiggi Mendoza 这是我使用您建议的 WebClient 得到的结果 System.dll 中发生了“System.Net.WebException”类型的异常,但未在用户代码中处理其他信息:远程服务器返回错误:(404)未找到。
-
@LuiggiMendoza 实际上我得到了它的工作错误来自这行代码。字符串 HtmlResult = wc.UploadString(Url, postData.ToString());页面完成了它的工作,只是我没有收到响应并且我的应用程序收到上述错误。当我注释掉 wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; --- 这一行,,,我没有收到错误,但页面没有完成它的工作 -
-
远程服务器返回错误:(404) Not Found 这意味着您访问了错误的页面或该页面不存在。您还应该尝试对错误消息进行一些研究。
-
@LuiggiMendoza 请阅读我的最后评论