gpt4 book ai didi

java - Java 中的 ServicePointManager 等价物

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:05 26 4
gpt4 key购买 nike

我正在尝试将 VB.net 项目转换为用于 Android 应用程序的 Java。我在某个时候卡住了。我的 VB.net 代码是

Public Function SendWebRequest(ByVal url As String, ByVal postData As String, ByVal TimeOut As String, ByVal Code As String) As String
Dim result As String
Try


postData = "Some String" + postData


Dim webRequest As WebRequest = webRequest.Create(url)
webRequest.Method = "POST"
webRequest.Timeout = IntegerType.FromString(TimeOut)
webRequest.Headers.Add(name1, value1)

'problem is here
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3

Dim bytes As Byte() = System.Text.Encoding.UTF8().GetBytes(postData) 'Encoding.get_UTF8().GetBytes(postData)
webRequest.ContentType = "application/x-www-form-urlencoded"
webRequest.ContentLength = CLng(bytes.Length()) 'CLng(bytes.get_Length())
Dim stream As Stream = webRequest.GetRequestStream()
'stream.Write(bytes, 0, bytes.get_Length())
stream.Write(bytes, 0, bytes.Length())
stream.Close()
Dim response As WebResponse = webRequest.GetResponse()
stream = response.GetResponseStream()
Dim streamReader As StreamReader = New StreamReader(stream)
Dim text As String = streamReader.ReadToEnd()
streamReader.Close()
stream.Close()
response.Close()
result = text
Catch expr_1AA As Exception
Dim ex As Exception = expr_1AA
Console.WriteLine("Exception ReadSecConn:" + ex.Message())
End Try
Return result
End Function

此代码发送网络请求。我已经使用 json 成功地将 web 请求从我的 android 应用程序发送到 web 服务器。除了这两行,所有部分都清楚

 ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3

有谁知道它在 java 中的等价物,您的帮助将不胜感激提前致谢

最佳答案

经过大量搜索和学习后,我得出结论,没有直接等同于 ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
但是要将安全协议(protocol)设置为 SSL3,我们可以使用

System.setProperty("https.protocols", "SSLv3");

希望对你有帮助

关于java - Java 中的 ServicePointManager 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24646384/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com