none
[UWP] Upgrading stream socket to secured socket windows mobile 10 app using UpgradeToSslAsync RRS feed

  • Question

  • I am trying to connect to a proxy from Windows Mobile 10 Application and retrieve https data by doing a sslupgrade:  There are several links available on the web , but none of the solution is working..

    data to be retrieved https://news.google.com. 

    I get the following error:

    The token supplied to the function is invalid (Exception from HRESULT: 0x80090308)

    Source code given below:

    ---------------------------

    Windows.Networking.HostName serverHost = new Windows.Networking.HostName(PROXY_IP);

    request = "CONNECT news.google.com:443 HTTP/1.0\r\nUser-Agent: NativeHost\r\nHost: news.google.com:443\r\nContent-Length: 0\r\nProxy-Connection: Keep-Alive\r\nPragma: no-cache\r\n\r\n";

    string serverPort = PROXY_PORT.ToString();

    await socket.ConnectAsync(serverHost, serverPort, SocketProtectionLevel.PlainSocket);

    Stream streamOut = socket.OutputStream.AsStreamForWrite();
    StreamWriter writer = new StreamWriter(streamOut);
    await writer.WriteLineAsync(request.ToString());
    await writer.FlushAsync();
    HostName destHost = new HostName("news.google.com");

    try
    {

    await socket.UpgradeToSslAsync(SocketProtectionLevel.Ssl, destHost);  //fails here

    }

    catch (Exception ex)
    {
    Debug.WriteLine("Socket upgrade failed" + ex.Message);

    }

    //then sending Http request msg to receive data

    Can somebody please help me , whats wrong here.

    Regards,

    Sharmistha

    Tuesday, February 28, 2017 2:57 PM

All replies

  • I am trying to connect to a proxy from Windows Mobile 10 Application and retrieve https data by doing a sslupgrade:

    data to be retrieved https://news.google.com. 

    I get the following error:


    The token supplied to the function is invalid (Exception from HRESULT: 0x80090308)

    Source code given below:

    ---------------------------

    Windows.Networking.HostName serverHost = new Windows.Networking.HostName(PROXY_IP);

    request = "CONNECT news.google.com:443 HTTP/1.0\r\nUser-Agent: NativeHost\r\nHost: news.google.com:443\r\nContent-Length: 0\r\nProxy-Connection: Keep-Alive\r\nPragma: no-cache\r\n\r\n";

    string serverPort = PROXY_PORT.ToString();

    await socket.ConnectAsync(serverHost, serverPort, SocketProtectionLevel.PlainSocket);

                    Stream streamOut = socket.OutputStream.AsStreamForWrite();
                    StreamWriter writer = new StreamWriter(streamOut);
                    await writer.WriteLineAsync(request.ToString());
                    await writer.FlushAsync();
                    HostName destHost = new HostName("news.google.com");

                    try
                    {

                        await socket.UpgradeToSslAsync(SocketProtectionLevel.Ssl, destHost);

                    }

                    catch (Exception ex)
                    {
                        Debug.WriteLine("Socket upgrade failed" + ex.Message);

                    }


    Friday, February 24, 2017 1:17 PM
  • Hi,

    As Prashant said in the following thread, “you will need to completely read the response before you call UpgradeToSslAsync(). If there is a pending read operation in progress, you cannot just call UpgradeToSslAsync in the middle of a read/write.”
    https://social.msdn.microsoft.com/Forums/en-US/abdf1b6c-afc7-43a0-b54c-60d2b025435e/how-to-use-upgradetosslasync-over-a-proxy-server-using-a-stream-socket?forum=winappswithnativecode 

    Best Regards,
    David

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 1, 2017 8:20 AM
  • Hi,

    I could upgrade the socket but unable to read the bytes from socket for a HTTP GET request after doing a SSL Upgrade as it is encrypted.. While SSL Upgrade is working but ReadAsync hangs when theres no more data In the debugger it shows couple of threads exited. Read around 102 KB of data including certificates

    Can someone please help to understand when we should break without ReadAsync getting hanged and how to decrypt the certificate and encrypted data received on the socket

                   

                    Windows.Networking.Sockets.StreamSocket socket = new Windows.Networking.Sockets.StreamSocket();
                    socket.Control.KeepAlive = true;

                    Windows.Networking.HostName serverHost = new Windows.Networking.HostName(PROXY_IP);


                    string serverPort = PROXY_PORT.ToString();

                    await socket.ConnectAsync(serverHost, serverPort, SocketProtectionLevel.PlainSocket);
                    string request = "CONNECT news.google.com:443 HTTP/1.1\r\nUser-Agent: NativeHost\r\nHost: news.google.com:443\r\nContent-Length: 0\r\nProxy-Connection: Keep-Alive\r\nPragma: no-cache\r\n\r\n";


                    Stream streamOut = socket.OutputStream.AsStreamForWrite();
                    StreamWriter writer = new StreamWriter(streamOut);
                    await writer.WriteLineAsync(request.ToString());
                    await writer.FlushAsync();
                    HostName destHost = new HostName("news.google.com");



                    byte[] responseBytes = new byte[1024];
                    Array.Clear(responseBytes, 0, responseBytes.Length);
                    IInputStream inputStream = socket.InputStream;


                    await inputStream.ReadAsync(responseBytes.AsBuffer(), (uint)responseBytes.Length, InputStreamOptions.Partial);
                    string connStr = Convert.ToBase64String(responseBytes);

                    Debug.WriteLine("Connection Establihed", (uint)responseBytes.Length);
                    Debug.WriteLine(connStr);


                    try
                    {
                        Debug.WriteLine("upgrading ssl");
                        await socket.UpgradeToSslAsync(SocketProtectionLevel.Tls11, destHost);

                    }

                    catch (Exception ex)
                    {
                        Debug.WriteLine("Socket upgrade failed" + ex.Message);

                    }

                    Debug.WriteLine("socket UPGRADE DONE");

                    nrequest = "GET news.google.com:443 HTTP/1.1\r\nHost:news.google.com:443\r\nProxy-Connection: Keep-Alive\r\n\r\n";
                    streamOut = socket.OutputStream.AsStreamForWrite();
                    await writer.WriteLineAsync(snirequest.ToString());
                    await writer.FlushAsync();

                    Debug.WriteLine("Sent GET Req");

                    responseBytes = new byte[1024];

                    Array.Clear(responseBytes, 0, responseBytes.Length);
                    await inputStream.ReadAsync(responseBytes.AsBuffer(), (uint)responseBytes.Length, InputStreamOptions.Partial);

                    string respStr = Convert.ToBase64String(responseBytes);


                    Debug.WriteLine(respStr);
                    while (responseBytes.Length != 0)
                    {

                        try
                        {
                            Array.Clear(responseBytes, 0, responseBytes.Length);
                            IBuffer asyncreader = await inputStream.ReadAsync(responseBytes.AsBuffer(), (uint)responseBytes.Length, InputStreamOptions.Partial);  // hangs after getting data couple of time..
                            Debug.WriteLine("read agaian");

                        }

                        catch (Exception ex)
                        {
                            Debug.WriteLine("ReadAsync failed" + ex.Message);

                        }

                        if (responseBytes.Length == 0)
                        {
                            Debug.WriteLine("breaking nothing more to read");
                            break;

                        }
                        respStr = Convert.ToBase64String(responseBytes);
                        Debug.WriteLine(respStr);
                        Debug.WriteLine("ReadAsync Over read bytes {0}", (uint)responseBytes.Length);
                        nrequest = "GET news.google.com:443 HTTP/1.1\r\nHost:news.google.com:443\r\nProxy-Connection: Keep-Alive\r\n\r\n";
                        streamOut = socket.OutputStream.AsStreamForWrite();
                        await writer.WriteLineAsync(nrequest.ToString());
                        await writer.FlushAsync();


                    }

                    Debug.WriteLine("End of while");

                }

    Regards,

    Sharmistha




    Thursday, March 2, 2017 10:37 AM
  • Hi,

    I could upgrade the socket but unable to read the bytes from socket for a HTTP GET request after doing a SSL Upgrade as it is encrypted.. While SSL Upgrade is working but ReadAsync hangs when theres no more data In the debugger it shows couple of threads exited. Read around 102 KB of data including certificates

    Can someone please help to understand when we should break without ReadAsync getting hanged and how to decrypt the certificate and encrypted data received on the socket

                   

                    Windows.Networking.Sockets.StreamSocket socket = new Windows.Networking.Sockets.StreamSocket();
                    socket.Control.KeepAlive = true;

                    Windows.Networking.HostName serverHost = new Windows.Networking.HostName(PROXY_IP);


                    string serverPort = PROXY_PORT.ToString();

                    await socket.ConnectAsync(serverHost, serverPort, SocketProtectionLevel.PlainSocket);
                    string request = "CONNECT news.google.com:443 HTTP/1.1\r\nUser-Agent: NativeHost\r\nHost: news.google.com:443\r\nContent-Length: 0\r\nProxy-Connection: Keep-Alive\r\nPragma: no-cache\r\n\r\n";


                    Stream streamOut = socket.OutputStream.AsStreamForWrite();
                    StreamWriter writer = new StreamWriter(streamOut);
                    await writer.WriteLineAsync(request.ToString());
                    await writer.FlushAsync();
                    HostName destHost = new HostName("news.google.com");



                    byte[] responseBytes = new byte[1024];
                    Array.Clear(responseBytes, 0, responseBytes.Length);
                    IInputStream inputStream = socket.InputStream;


                    await inputStream.ReadAsync(responseBytes.AsBuffer(), (uint)responseBytes.Length, InputStreamOptions.Partial);
                    string connStr = Convert.ToBase64String(responseBytes);

                    Debug.WriteLine("Connection Establihed", (uint)responseBytes.Length);
                    Debug.WriteLine(connStr);


                    try
                    {
                        Debug.WriteLine("upgrading ssl");
                        await socket.UpgradeToSslAsync(SocketProtectionLevel.Tls11, destHost);

                    }

                    catch (Exception ex)
                    {
                        Debug.WriteLine("Socket upgrade failed" + ex.Message);

                    }

                    Debug.WriteLine("socket UPGRADE DONE");

                    nrequest = "GET / HTTP/1.1\r\nHost:news.google.com:443\r\nProxy-Connection: Keep-Alive\r\n\r\n";
                    streamOut = socket.OutputStream.AsStreamForWrite();
                    await writer.WriteLineAsync(snirequest.ToString());
                    await writer.FlushAsync();

                    Debug.WriteLine("Sent GET Req");

                    responseBytes = new byte[1024];

                    Array.Clear(responseBytes, 0, responseBytes.Length);
                    await inputStream.ReadAsync(responseBytes.AsBuffer(), (uint)responseBytes.Length, InputStreamOptions.Partial);

                    string respStr = Convert.ToBase64String(responseBytes);


                    Debug.WriteLine(respStr);
                    while (responseBytes.Length != 0)
                    {

                        try
                        {
                            Array.Clear(responseBytes, 0, responseBytes.Length);
                            IBuffer asyncreader = await inputStream.ReadAsync(responseBytes.AsBuffer(), (uint)responseBytes.Length, InputStreamOptions.Partial);  // hangs after fetching data couple of times
                            Debug.WriteLine("read agaian");

                        }

                        catch (Exception ex)
                        {
                            Debug.WriteLine("ReadAsync failed" + ex.Message);

                        }

                        if (responseBytes.Length == 0)
                        {
                            Debug.WriteLine("breaking nothing more to read");
                            break;

                        }
                        respStr = Convert.ToBase64String(responseBytes);
                        Debug.WriteLine(respStr);
                        Debug.WriteLine("ReadAsync Over read bytes {0}", (uint)responseBytes.Length);
                        nrequest = "GET news.google.com:443 HTTP/1.1\r\nHost:news.google.com:443\r\nProxy-Connection: Keep-Alive\r\n\r\n";
                        streamOut = socket.OutputStream.AsStreamForWrite();
                        await writer.WriteLineAsync(nrequest.ToString());
                        await writer.FlushAsync();


                    }

                    Debug.WriteLine("End of while");

                }

    Regards,

    Sharmistha



    Thursday, March 2, 2017 10:44 AM
  • Just out of curiosity, did you ever get this solved?
    Thursday, November 7, 2019 3:51 PM