0

I am writing a web application using asp.net. I connect to a JSP page through some network that is not public but available to my server side. My goal is from my asp.net page (which will be public) post data to a jsp page (which is private for users) and then retrieve the answer of the 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();
}
}
user3410843
  • 195
  • 3
  • 18
  • What have you tried? Split this into two concerns: 1) Your ASP.NET application accepts input from a user. 2) Your server-side code sends a request to an external resource. Both are separate. You can use something like `HttpClient` in .NET to send requests to the external resource and read the response. – David Mar 13 '14 at 15:46
  • #Luiggi Mendoza This is what I get using the WebClient that you suggested An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code Additional information: The remote server returned an error: (404) Not Found. – user3410843 Mar 13 '14 at 16:27
  • @LuiggiMendoza actually I got it working the error is from this line of code. string HtmlResult = wc.UploadString(Url, postData.ToString()); page has done its job only I dont recieve response and my applications gets the above error. When I comment out wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; --- this line,,, I get no errors but the page is not doing its job – – user3410843 Mar 13 '14 at 17:00
  • *The remote server returned an error: (404) Not Found* this means you're accessing to a wrong page or the page doesn't exist. You should also try doing some research about the error messages. – Luiggi Mendoza Mar 13 '14 at 17:02
  • @LuiggiMendoza Please read my last comment – user3410843 Mar 13 '14 at 17:04
  • *I dont recieve response and my applications gets the above error* which means you still receive a 404 - Not found. – Luiggi Mendoza Mar 13 '14 at 17:05
  • By the way, you should not add that header about `application/x-www-form-urlencoded` since you're not performing a file upload. – Luiggi Mendoza Mar 13 '14 at 17:07

1 Answers1

0

@LuiggiMendosa but when I checked the result I understood that the page has worked as I say page has done it's job only I dont recieve response and my applications gets the above error and when i change HttpRequestHeader.ContentType the page is not do anything.

Rapira
  • 83
  • 1
  • 7