HttpWebREsponse problem in ASP.net
Hello everybody
I am developing a website in ASP.net.i have one htm page where the user enters the userid and password.on submitting the page i am redirecting it to an aspx page where in i am facin some problems.the ConnectServer.aspx page actully connects to a servlet on our server.I pass the username and password as parmeters to the servlet.the servlet validates the user and sends back some response.Now i am able to call the servlet properly using HTTPWebRequest.but i am having problems in capturing the Response from the Servlet using HTTPWebResponse.the problem is that when i click submit i am redirected to the ConnectServer.aspx page but i do not get any response displayed on my screen.I have used Response.write to see the response from servlet on the screen.however whe i refresh the page from i am able to view the data.i just dont knwo what the problem is.sometimes response is displayed and sometimes not.please help me.. i am also sending the code.please help coz its really urgent.
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Data.OleDb
Partial Class ConnectServer
Inherits System.Web.UI.Page
Dim strparam As String
Dim req As HttpWebRequest
Dim resp As HttpWebResponse = Nothing
Dim objStream As Stream
Dim url As String
Dim struser As String
Dim strpwd As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
url= "url of sevlet"
struser = Request.Form("txtusername")
strpwd = Request.Form("txtpassword")
Response.Write("Username is " + struser + "Password is " + strpwd)
'Dim prxy As New WebProxy("10.0.0.1", 8085)
strparam = ("username=" & struser & "&password=" & strpwd)
Dim encoding As New ASCIIEncoding()
Dim enc = System.Text.Encoding.GetEncoding(1252)
Dim byte1 As Byte() = encoding.GetBytes(strparam)
req = WebRequest.Create(url)
'req.Proxy = prxy
req.Method = "POST"
req.ContentLength = strparam.Length
req.ContentType = "application/x-www-form-urlencoded"
Try
Dim newStream As Stream = req.GetRequestStream()
newStream.Write(byte1, 0, byte1.Length)
'Response.Write("The value of 'ContentLength' property after sending the data is {0}" + req.ContentLength)
newStream.Close()
Catch ex As Exception
Response.Write("Error in Request " & ex.Message.ToString())
End Try
End Sub
Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
Try
url = "url of servlet"
req = WebRequest.Create(url)
resp = CType(req.GetResponse(), HttpWebResponse)
req.Method = "GET"
req.ContentType = "application/x-www-form-urlencoded"
If (resp.StatusCode <> HttpStatusCode.OK) Then
Throw New Exception(Response.StatusCode.ToString())
End If
objStream = req.GetResponse.GetResponseStream()
Dim objReader As New StreamReader(objStream)
Dim sLine As String = ""
Dim i As Integer = 0
Do While Not sLine Is Nothing
i += 1
sLine = objReader.ReadLine
If Not sLine Is Nothing Then
Response.Write("Response from Servlet " + sLine)
End If
Loop
sLine = Nothing
objStream.Close()
objReader.Close()
resp.Close()
Catch ex As Exception
Response.Write("Error in Response " & ex.Message.ToString())
End Try
End Sub
End Class
thanx
parth
|