获得WCF服务流以https [英] Get a stream from WCF service with https
问题描述
我想使用Android客户端我的WCF服务下载图像。该连接采用SSL安全,所以我得给特殊权限,才能做到这一点。我不知道这是确定的,但这里是我有:
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();
以上是该方法的定义中,以图片,和下面是执行(ⅰ得到的varbinary字段在SQL Server包含在AttachmentData列中的图像)。
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;
}
}
在Android方面,我有这首先code信任任何SSL证书:
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);
}
}
终于搞定流:
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());
问题是,我立刻得到-1,则流的末尾,或许我不发送它的权利或接受它。任何帮助的人?
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返回之前重置流位置:
I solved this by resetting the stream position before returning it from WCF:
stream.Position = 0;
这篇关于获得WCF服务流以https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!