我在写一个使用asp.net的web应用程序。我通过一些非公开但可用于服务器端的网络连接到JSP页面。我的目标是从我的asp.net页面(这将是大众)发布的数据到JSP页面(这是私人用户),然后检索jsp.page将数据从ASP.net页面发布到JSP页面
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'的东西来发送请求到外部资源并读取响应。 – David
#Luiggi门多萨 这是我得到使用你的建议 型“System.Net.WebException”发生在System.dll中的一个例外,但在用户代码 其他信息没有处理Web客户端:远程服务器返回错误:(404)未找到。 – user3410843
@LuiggiMendoza其实我得到它的工作错误是从这行代码。字符串HtmlResult = wc.UploadString(Url,postData.ToString());页面已完成其工作,但我没有收到响应,我的应用程序得到了上述错误。当我注释掉wc.Headers [HttpRequestHeader.ContentType] =“application/x-www-form-urlencoded”; ---这行,,,我没有得到任何错误,但页面没有完成它的工作 - – user3410843