Quantcast
Channel: Help with function
Viewing all articles
Browse latest Browse all 6

Help with function

$
0
0

I have the next function wich i use to send an xml file named "result.xml" to a webservice, i get an exception at "Dim reqStream As System.IO.Stream = webRequest.GetRequestStream()" wich says "The underlying connection was closed: The remote name could not be resolved"

 

Can anyone help me with this?

 

Function MakeHTTPPost(ByVal fullUrl AsString, OptionalByVal bAllowAutoRedirect AsBoolean = True, OptionalByVal iTimeout AsInteger = System.Threading.Timeout.Infinite) AsString

Dim webRequest As System.Net.HttpWebRequest

Dim webResponse As System.Net.HttpWebResponse

Dim localFile AsString = "result.xml"

Try

'Create an HttpWebRequest with the specified URL.

webRequest =

CType(System.Net.WebRequest.Create(fullUrl), System.Net.HttpWebRequest)

webRequest.ContentType = "text/xml"

webRequest.Method = "POST"

webRequest.AllowAutoRedirect = bAllowAutoRedirect

'webRequest.MaximumAutomaticRedirections = 50

webRequest.Timeout = iTimeout

Dim reqStream As System.IO.Stream = webRequest.GetRequestStream()

Dim wrtr As System.IO.StreamWriter = New System.IO.StreamWriter(reqStream)

'Open the local file

Dim rdr As System.IO.StreamReader = New System.IO.StreamReader(localFile)

'loop through the local file reading each line

'and writing to the request stream buffer

Dim inLine AsString = rdr.ReadLine()

While (inLine <> "")

wrtr.WriteLine(inLine)

inLine = rdr.ReadLine()

EndWhile

rdr.Close()

wrtr.Close()

'Send the request and wait for a response.

Try

webResponse =

CType(webRequest.GetResponse(), System.Net.HttpWebResponse)

SelectCase (webResponse.StatusCode)

Case System.Net.HttpStatusCode.OK

'read the content from the response

Dim responseStream As System.IO.Stream = webResponse.GetResponseStream()

Dim responseEncoding As System.Text.Encoding = System.Text.Encoding.UTF8

' Pipes the response stream to a higher level stream reader with the required encoding format.

Dim responseReader AsNew System.IO.StreamReader(responseStream, responseEncoding)

Dim responseContent AsString = responseReader.ReadToEnd()

Return responseContent

Case System.Net.HttpStatusCode.Redirect, System.Net.HttpStatusCode.MovedPermanently

ThrowNew System.Exception(String.Format("Unable to read response content. URL has moved. StatusCode={0}.", webResponse.StatusCode))

Case System.Net.HttpStatusCode.NotFound

ThrowNew System.Exception(String.Format("Unable to read response content. URL not found. StatusCode={0}.", webResponse.StatusCode))

CaseElse

ThrowNew System.Exception(String.Format("Unable to read response content. StatusCode={0}.", webResponse.StatusCode))

EndSelect

Catch we As System.Net.WebException

ThrowNew System.Exception("Unable to execute URL.", we)

Finally

If (Not IsNothing(webResponse)) Then

webResponse.Close()

EndIf

EndTry

Catch e As System.Exception

MsgBox("Unable to execute URL2. - " & e.Message)

EndTry

EndFunction

Viewing all articles
Browse latest Browse all 6

Trending Articles