简体   繁体   中英

Get a stream from WCF service with https

I am trying to download an image from my wcf service using android client. The connection is secured with ssl, so I have to give special permissions to do this. I am not sure this is ok, but here is what I have:

 [OperationContract]
    [WebGet(UriTemplate="getstream")]
    Stream GetStream();

Above is the definition of the method to get the picture, and below is the implementation ( i get the image from varbinary field in sql server contained in the AttachmentData column).

public Stream GetStream()
    {
        using (MojDBEntities ctx = new MojDBEntities())
        {
            var image = from attach in ctx.JournalAttachments
                        where attach.JournalAttachmentID == 747
                        select attach;

            List<JournalAttachment> ls = image.ToList();
            JournalAttachment temp = ls.ElementAt(0);
            byte[] myByteArray = temp.AttachmentData;
            MemoryStream stream = new MemoryStream(); 
            stream.Write(myByteArray, 0, myByteArray.Length);
            return stream;
        }
    }

On the android side, I have firstly this code to trust any ssl certificate:

    public void TrustALL() throws IOException, NoSuchAlgorithmException, KeyManagementException{
    TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
        public void checkClientTrusted(X509Certificate[] certs, String authType) {
        }
        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }
    }
};

// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
};

// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
}

}

and finally to get the stream:

TrustALL();
            URL imageUrl = new URL("https://192.168.10.103/RcaRestService/RCAService.svc/getstream");
            HttpURLConnection conn = (HttpURLConnection) imageUrl
                    .openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is = conn.getInputStream();

            Log.i("donnnn", ""+is.read());

the problem is that I immediately get -1, end of the stream, perhaps I am not sending it right or receiving it. Any help anyone ?

我通过在从WCF返回之前重置流位置来解决此问题:

stream.Position = 0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM