VB.NET:设置ServicePointManager.Expect100Continue = False

时间:2015-08-27 15:04:24

标签: vb.net

我正在尝试设置以下属性但不知道在哪里放置代码,我已经尝试将它放在主窗体的顶部,但它只是说'预期声明'

System.Net.ServicePointManager.Expect100Continue = False

我正在使用VB.NET(Framework 4)

    Private Function sendAccReq(ByVal method As String)

    System.Net.ServicePointManager.Expect100Continue = False

    Dim request As WebRequest = WebRequest.Create("https://api.betfair.com/exchange/account/json-rpc/v1/")
    Dim postData As String = "{""jsonrpc"": ""2.0"", ""method"": ""AccountAPING/v1.0/" & method & """}"
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)

    request.Method = "POST"
    request.ContentType = "application/json"
    request.ContentLength = byteArray.Length
    request.Headers.Add("X-Application: " & appkey)
    request.Headers.Add("X-Authentication: " & sessionToken)

    Dim dataStream As Stream = request.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)
    dataStream.Close()

    Dim response As WebResponse = request.GetResponse()
    dataStream = response.GetResponseStream()

    Dim reader As New StreamReader(dataStream)
    Dim responseFromServer As String = reader.ReadToEnd()

    reader.Close()
    dataStream.Close()
    response.Close()

    Return responseFromServer

End Function

当我在调试Expect100Continue仍然显示为TRUE时进入请求的属性时,它仍然无效。

1 个答案:

答案 0 :(得分:0)

我在Betfair API上遇到了这个问题,我在创建每个Web请求之前添加了代码行。

System.Net.ServicePointManager.Expect100Continue = False 
Dim request = CreateWebRequest(New Uri(EndPoint))

它修复了问题,但我不能100%确定它是否适合放置它。 在发出网络请求后,您是否收到任何错误?

更新:

我的代码和您的代码之间存在一些差异,下面是我创建网络请求的方式:

Dim request As WebRequest = WebRequest.Create(New Uri(EndPoint))
request.Method = "POST"
request.ContentType = "application/json-rpc"
request.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8")

你看过GitHub上的代码示例,(我知道它们是C#但可能有帮助:) https://github.com/betfair/API-NG-sample-code/blob/master/cSharp/Api-ng-sample-code/Api-ng-sample-code/JsonRpcClient.cs

让我知道你是怎么过的,