473,569 Members | 2,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HttpWebResponse and HttpWebRequest not working on local machine

4 New Member
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 3330
Plater
7,872 Recognized Expert Expert
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
JVNewbie
4 New Member
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
JVNewbie
4 New Member
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
JVNewbie
4 New Member
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
3203
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 in a variable and make the request again with that cookie as a header value (Digest Authentication). But, the problem we are facing is: That the...
0
2100
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 for a corresponding HTTPWebResponse. This works just fine. However, the timeout does not appear to apply to a BinaryReader created from the...
0
2912
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 same machine (for dev and testing). Here is the C# code snippet: 1 string clientAddr = "http://127.0.0.1:22225/"; 2 try 3 { 4 webreq =...
3
7362
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) supplied by the remote Webserver I can successfuly logon but when I redirect to the supplied url, the webserver does not know me anymore an redirects...
1
2787
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 in a variable and make the request again with that cookie as a header value (Digest Authentication). But, the problem we are facing is: That the...
2
2593
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 back the single string it returns (OK/Not-Ok)! It seems such a simple thing to do, but I've been on this for hours now! Here's the code I'm...
2
14199
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 auxilliary files such as these must be downloaded "manually" in my code. My text-based files download fine but I've ran into a problem downloading an...
0
1523
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 that contains a frame loading content from another domain. Any insight would be welcome. The Web Page: I try to hit a url (see code below) say...
1
4331
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 production server (MS Win 2k) I am getting the following socket error: System.Net.Sockets.SocketException: No connection could be made because
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.