473,396 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

HttpWebResponse and HttpWebRequest not working on local machine

I'm attempting to test an aspx module that will receive XML data from a web service (the receiver module). I want to be able to test this portion before attempting to create the Web Service that will generate the POST to this module. The "poster module" works fine but when I try to get the response back from the receiver module I get a "(500) Internal Server Error." and the module is never executed. I have added it to the same solution and also attempted as a separate solution on my machine. The path is correct. What am I doing wrong?

poster module:

Expand|Select|Wrap|Line Numbers
  1.  
  2.    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  3.  
  4.         Dim request As HttpWebRequest
  5.         Dim response As HttpWebResponse = Nothing
  6.         Dim reader As StreamReader
  7.  
  8.         Try
  9.             ' Create the web request   
  10.             request = DirectCast(WebRequest.Create("http://localhost/ISOClaimWebServices/SampleMatchResponse.xml"), HttpWebRequest)
  11.  
  12.             ' Get response   
  13.             response = DirectCast(request.GetResponse(), HttpWebResponse)
  14.  
  15.             ' Get the response stream into a reader   
  16.             reader = New StreamReader(response.GetResponseStream())
  17.  
  18.             Call Post(reader.ReadToEnd)
  19.  
  20.         Catch ex As WebException
  21.  
  22.             txtErrorMessage.Text = ex.Message
  23.  
  24.         Finally
  25.  
  26.             If Not response Is Nothing Then
  27.                 response.Close()
  28.             End If
  29.  
  30.         End Try
  31.  
  32.     End Sub
  33.  
  34.    Private Sub Post(ByVal PostData As String)
  35.  
  36.         'Dim sr As StreamReader = New StreamReader("c:\test.xml")
  37.         'Dim XMLRead As String = sr.ReadToEnd
  38.         'Dim stXML As String
  39.         'stXML = XMLRead
  40.         'sr.Close()
  41.  
  42.         Try
  43.  
  44.             Dim results As String
  45.  
  46.             Dim request As WebRequest = WebRequest.Create("http://localhost/ISOClaimResponse/ISOClaimResponse.aspx ")
  47.  
  48.             request.Method = "POST"
  49.             request.ContentType = "text/xml; charset=utf-8"
  50.             results = WriteToURL(request, PostData)
  51.  
  52.             Dim Response As String
  53.  
  54.             Response = RetrieveFromURL(request)
  55.  
  56.         Catch ex As WebException
  57.  
  58.             txtErrorMessage.Text = ex.Message
  59.  
  60.         End Try
  61.  
  62.     End Sub
  63.  
  64.     Private Function WriteToURL(ByVal Request As WebRequest, ByVal data As String) As String
  65.  
  66.         Try
  67.  
  68.             Dim bytes = System.Text.Encoding.ASCII.GetBytes(data)
  69.             Request.ContentLength = bytes.Length
  70.  
  71.             Dim OutPutStream As Stream = Request.GetRequestStream
  72.             OutPutStream.Write(bytes, 0, bytes.length)
  73.             OutPutStream.Close()
  74.  
  75.         Catch ex As WebException
  76.  
  77.             txtErrorMessage.Text = ex.Message
  78.  
  79.         End Try
  80.  
  81.     End Function
  82.  
  83.     Private Function RetrieveFromURL(ByVal Request As WebRequest) As String
  84.  
  85.         Try
  86.  
  87.             Dim response As WebResponse = Request.GetResponse
  88.             Dim stream As Stream = response.GetResponseStream
  89.             Dim sr As StreamReader = New StreamReader(stream)
  90.  
  91.             Return sr.ReadToEnd
  92.  
  93.         Catch ex As WebException
  94.  
  95.             If ex.Status = WebExceptionStatus.ProtocolError Then
  96.                 txtErrorMessage.Text = ex.Message
  97.             End If
  98.  
  99.         End Try
  100.  
  101.     End Function
  102.  
  103. receiver module:
  104.  
  105.    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  106.  
  107.         Call GetResponse1()
  108.  
  109.     End Sub ' Main
  110.  
  111.     Private Sub GetResponse1()
  112.  
  113.         '  Get the response.
  114.         Dim WebResponse As HttpWebResponse
  115.         Dim streamResponse As Stream = WebResponse.GetResponseStream()
  116.         Dim streamRead As New StreamReader(streamResponse)
  117.         Dim responseString As String = streamRead.ReadToEnd()
  118.  
  119.         lblResponseContent.Text = responseString
  120.  
  121.         ' Close Stream object.
  122.         streamResponse.Close()
  123.         streamRead.Close()
  124.  
  125.         'Return Response to poster.
  126.         response.Write("XML RECEIVED")
  127.  
  128.         ' Release the HttpWebResponse.
  129.         WebResponse.Close()
  130.  
  131.     End Sub
  132.  
  133.  
Thanks for any help.
Aug 21 '07 #1
4 3300
Plater
7,872 Expert 4TB
You use localhost a lot, is the computer that the code is running on really the computer you are trying to talk to?
Is testing this code keeping the section that you wish to talk to from coming up and answering?
A 500 class error usually means whoever you are trying to talk to is having problems.

You also have a duplicate function definition in there (page_load)
Aug 21 '07 #2
Thank you for your reply. They are two different modules and I have marked them as such. One is the poster and the other the receiver. Yes they are in my local machine.
Aug 21 '07 #3
Changed the receiver code and problem solved. This code is also working within the same solution so you can test and debug both modules before puting the receiver on the server. For those that may need this code here it is:

Receiver module:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  3.  
  4.         Call GetResponse()
  5.  
  6.     End Sub ' Main
  7.  
  8.     Private Sub GetResponse()
  9.  
  10.         Try
  11.  
  12.             Dim mobjStream As Stream
  13.             Dim strmContents As String
  14.             Dim counter, strLen, strRead As Integer
  15.  
  16.             ' Create a Stream object.
  17.             mobjStream = Request.InputStream
  18.  
  19.             ' Find number of bytes in stream.
  20.             strLen = CInt(mobjStream.Length)
  21.  
  22.             ' Create a byte array.
  23.             Dim strArr(strLen) As Byte
  24.  
  25.             ' Read stream into byte array.
  26.             strRead = mobjStream.Read(strArr, 0, strLen)
  27.  
  28.             ' Convert byte array to a text string.
  29.             For counter = 0 To strLen - 1
  30.                 strmContents = strmContents & strArr(counter).ToString()
  31.             Next counter
  32.  
  33.             lblResponseContent.Text = strmContents
  34.  
  35.             'Return Response to poster.
  36.             Response.Write("XML RECEIVED")
  37.  
  38.         Catch ex As Exception
  39.  
  40.             lblResponseContent.Text = ex.Message
  41.  
  42.         End Try
  43.  
  44.     End Sub
  45.  
Aug 21 '07 #4
One more thing, you'll need these imports:

Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.IO
  3. Imports System.Net
  4. Imports System.Text
  5.  
Aug 21 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Satinderpal Singh | last post by:
Hi everyone, We are using HttpWebRequest to create a request to a URI, which requires us to login first. In order to process all the transactions, first we have to login and get the cookie value...
0
by: Denny Rue | last post by:
I’ve created VB code to download files from a web site through the use of HTTPWebRequest, HTTPWebResponse and BinaryReader. The HTTPWebRequest has a TimeOut property to limit how long it waits...
0
by: Nathan | last post by:
This is a copy of a message at microsoft.public.dotnet.framework.clr: THE CODE: I'm using an HttpWebResponse object to send an HTTP POST to a Java server I have written and are running on the...
3
by: Karsten Grombach | last post by:
Hi, I'm trying the following: - Imitate a Logon using a Post with HttpWebRequest on remote Webserver (asp 3.0 page using https) - On success redirect to the page (encapsuled in an iframe)...
1
by: Satinderpal Singh | last post by:
Hi everyone, We are using HttpWebRequest to create a request to a URI, which requires us to login first. In order to process all the transactions, first we have to login and get the cookie value...
2
by: Noggin The Nog | last post by:
Hi all, I've been trying to get HttpWebResponse to work, but whenever I try I get "The operation has timed-out". I'm simply trying to send an HTTP request querystring to a remote website and read...
2
by: STEVE.KING | last post by:
Greetings: I need to download an executable and some other binary files (a C++ DLL) from a virtual directory in my WinForms application. No-Touch Deployment manages my .NET code fine but...
0
by: Howeird | last post by:
Problem: I am writting a quick and dirty web link checker for a site that allows local business to post their web site's URL. I have encountered a probelm when the reqested URI points to a site...
1
by: daz_oldham | last post by:
Hi I have a method (very bottom of this post) which I am using for doing XMLRPC calls. This works fine locally in Cassini (debug mode in VS2005, WinXP SP2), however when I run this on my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.