Ok, firstly let me say that accessing web services is whole new game for me, I have next to no knowledge of this side of things. I have a need to write a small app to enable a user to upload a file to a nominated web service and really have no idea of what is required. I have cobbled together some code (see below) that I have sampled from various places, it runs, but it does not upload the file. I suspect that I am connecting to the web service, as I am no longer receiving Access Denied errors :) Any help on getting the correct code to upload the file would be appreactiated.
Private Sub Process_XML(ByVal strXML As String, ByVal strStatus As String) Dim UserCredentials As NetworkCredential Dim strTestUrl As String = "https://webservice.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/LodgeDRS.wsdl"
Try
'ok, setup credentials for authentication
UserCredentials = New NetworkCredential("FGS243_WS", "xxxxxxxxxx")
Select Case strStatus
Case "TEST"
UploadXML(strTestUrl, UserCredentials)
Case "PRODUCTION"
End Select
Catch ex As Exception
Error_Message("Process_XML")
End Try
End Sub
Private Sub UploadXML(ByVal uriName As String, ByVal creds As NetworkCredential) Try ' get the file name from the path Dim strFile As String = System.IO.Path.GetFileName(strXMLFileName)
' get the file information for the selected file
Dim fInfo As New FileInfo(strXMLFileName)
' get the length of the file to see if it is possible
' to upload it (with the standard 4096 kb limit)
Dim numBytes As Long = fInfo.Length
Dim dLen As Double = Convert.ToDouble(fInfo.Length / 1000000)
' look for an overrun on file size
If (dLen < 5) Then
' set up a filestream and binary reader for the file
Dim fStream As New FileStream(strXMLFileName, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fStream)
' convert the file to a byte array
Dim FileData As Byte() = br.ReadBytes(Convert.ToInt32(numBytes))
br.Close()
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(uriName), HttpWebRequest)
myHttpWebRequest.ContentType = "text/xml"
myHttpWebRequest.Method = "POST"
myHttpWebRequest.Credentials = creds
myHttpWebRequest.PreAuthenticate = True
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
Dim receiveStream As Stream = myHttpWebResponse.GetResponseStream()
Dim encode As System.Text.Encoding = System.Text.Encoding.GetEncoding("utf-8")
Dim readStream As New StreamReader(receiveStream, encode)
Dim strResponse As String = readStream.ReadToEnd()
readStream.Dispose()
receiveStream.Dispose()
Console.Write(strResponse)
Console.ReadLine()
fStream.Close()
fStream.Dispose()
MsgBox("Done")
Else
MsgBox("The file selected, " & strXMLFileName & ", exceeds the size limit for uploads")
End If
Catch ex As Exception
Error_Message("UploadXML")
End Try
End Sub
Aucun commentaire:
Enregistrer un commentaire