使用 VB.net 发送 JSON 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25190937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send JSON data using VB.net
提问by Mariano Romano
I am trying to send JSON data to a web service using VB.NET. I am using the System.Web.Script.Serialization library but it is not working. When I look at the web service, it is just displaying: {METHOD = getMACAddress}
我正在尝试使用 VB.NET 将 JSON 数据发送到 Web 服务。我正在使用 System.Web.Script.Serialization 库,但它不起作用。当我查看 Web 服务时,它只是显示:{METHOD = getMACAddress}
Private Function sendWebRequest()
Dim json As New JavaScriptSerializer
Dim request As HttpWebRequest = DirectCast(WebRequest.Create("http://192.168.1.1/scripts/service.php"), HttpWebRequest)
' Set the Method property of the request to POST.
request.Method = "POST"
request.KeepAlive = True
Dim Data As String = "{METHOD = getMACAddress}"
Dim postData As String = json.Serialize(Data)
MsgBox(Data, 0, "Info")
Dim byteData As Byte() = Encoding.UTF8.GetBytes(postData)
' Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded"
' Set the ContentLength property of the WebRequest.
request.ContentLength = byteData.Length
' Get the request stream.
Dim dataStream As Stream = request.GetRequestStream()
' Write the data to the request stream.
dataStream.Write(byteData, 0, byteData.Length)
' Close the Stream object.
dataStream.Close()
' Get the response.
Dim response As WebResponse = request.GetResponse()
' Display the status.
Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
' Get the stream containing content returned by the server.
dataStream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
Console.WriteLine(responseFromServer)
' Clean up the streams.
reader.Close()
dataStream.Close()
response.Close()
End Function
回答by Howard
Not exactly sure what you are trying to accomplish, but I believe what you are looking for are webmethods.
不完全确定您要完成什么,但我相信您正在寻找的是 webmethods。
EG: Default.aspx.vb
EG: Default.aspx.vb
Inherits System.Web.Services.WebService
<System.Web.Services.WebMethod(BufferResponse:=False)> _
Public Function AddInt(A as Integer, B as Integer) As Integer
return A + B
End Function
Then from your client use ensure that your post your parameters to the method using contentType: "application/json; charset=utf-8"
然后从您的客户端使用确保您将参数发布到使用 contentType 的方法:“application/json; charset=utf-8”
<script type="text/javascript">
function GetAddInt() {
$.ajax({
type: "POST",
url: "Default.aspx/AddInt",
data: '{A: 5, B: 7}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) { alert(response.d); },
failure: function(response) { alert('Error'); }
});
}
</script>
Although this is a simplistic example, you can expand this by returning structured data or objects. Serialization is handled for you.
尽管这是一个简单的示例,但您可以通过返回结构化数据或对象来扩展它。序列化是为您处理的。
Note: Its very important that you set the content type or .NET will return the data in XML.
注意:设置内容类型非常重要,否则 .NET 将返回 XML 格式的数据。

