Search code examples
javaandroidvb.netsslservicepointmanager

ServicePointManager equivalent in java


i am trying to convert a VB.net project into java for an android app.i am stuck at some point. My VB.net code is

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

this code sends a web request. i have successfully sent web request from my android app to web server using json. All part is clear except this two lines

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

Do any one know its equivalent in java, your help will be appreciated thanks in advance


Solution

  • After searching and learning a lot i conclude that there is no direct equivalent of ServicePointManager.Expect100Continue = True ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 But for setting security protocol to SSL3 we can use

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

    hope this might help u