Hi,
I'm setting up a website to act as our API to our internal database.
I think I'm going in the right direction but keep hitting a brick wall.
Server code and my test routine below.
Error2 = ProtocolError
Any pointers to resolve would be greatly appreciated!
Paul
What I have tried:
NOTE:
Just Added:
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
to Webserver config.
Now:
https://WEBSITE/service.asmx/TestRest
Returns:
<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://localhost:51561/APIServer/">{"qtest":{"response":"hello"}}</string>
Getting better but my test site still fails. Same error as below.
Server Code: Service.vb file:
<WebService(Namespace:="http://localhost:51561/APIServer/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function TestRest() As String
Dim sjon As String = "{"
sjon = sjon & """qtest"":{""response"":""hello""}"
sjon = sjon & "}"
Return sjon
End Function
End Class
Service.asmx file:
<%@ WebService Language="vb" CodeBehind="~/App_Code/Service.vb" Class="Service" %>
Calling the Webserver with this:
Dim returnedData As String = ""
Dim error2 As String = ""
Try
Dim DataToSend As String = ""
Dim urlLive As String = "https://WEBSITENAME/Service.asmx/TestRest"
Dim postAddress = New Uri(urlLive)
Dim request = DirectCast(WebRequest.Create(postAddress), HttpWebRequest)
request.Method = "POST"
request.ContentType = "application/json"
request.Headers.Add("AcceptTypes", "text/plain")
request.Headers.Add("Accept-Language", "en-US")
request.Headers.Add("UserAgent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)")
request.ContentLength = Len(DataToSend).ToString
request.ProtocolVersion = HttpVersion.Version11
Dim postByteData As Byte() = System.Text.UTF8Encoding.UTF8.GetBytes(DataToSend)
request.ContentLength = postByteData.Length
Using postStream As Stream = request.GetRequestStream()
postStream.Write(postByteData, 0, postByteData.Length)
End Using
Using resp = TryCast(request.GetResponse(), HttpWebResponse)
Dim reader = New StreamReader(resp.GetResponseStream())
returnedData = reader.ReadToEnd()
End Using
Catch exw As WebException
Try
Dim httpResponse As HttpWebResponse = CType(exw.Response, HttpWebResponse)
returnedData = "Error - " & CInt(httpResponse.StatusCode).ToString() & " - " & httpResponse.StatusCode.ToString()
error2 = exw.Status.ToString
Catch ex As Exception
returnedData = "Error - Site"
End Try
Catch ex As Exception
returnedData = "Error - Exception Error"
End Try
error2 = error2 & ""
returnedData = returnedData & ""