我需要从Java调用一个安全的WCF服务,并使用双向认证。所有东西都运行良好,除了我无法发送大于48680字节大小的消息。因此,成功发送了48680字节的消息,但是48681字节的消息没有发送成功,Java应用程序会出现读取超时异常,尽管WCF的配额设置允许发送更大的消息。
那么问题可能是什么?
编辑 源代码如下:
那么问题可能是什么?
编辑 源代码如下:
package foo.bar;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import javax.net.ssl.*;
import java.io.*;
import java.net.URL;
import java.security.KeyStore;
public class ReadTimedOutTest {
@Test
public void testReadTimedOut() throws Exception {
URL url = new URL("https://services/endpoint/");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
setUpSSL(connection);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("SOAPAction", "http://namespace/2012/01/service/Operation");
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
connection.setConnectTimeout(10 * 1000);
connection.setReadTimeout(10 * 1000);
connection.setInstanceFollowRedirects(true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
InputStream is = ReadTimedOutTest.class.getResourceAsStream("payload.failure.xml");
try {
IOUtils.copy(is, bos);
} finally {
is.close();
}
byte[] bytes = bos.toByteArray();
connection.setRequestProperty("Content-Length", String.valueOf(bytes.length));
OutputStream os = connection.getOutputStream();
try {
IOUtils.copy(new ByteArrayInputStream(bytes), os);
os.flush();
} finally {
os.close();
}
int respCode = connection.getResponseCode();
if(respCode >= HttpsURLConnection.HTTP_INTERNAL_ERROR) {
is = connection.getErrorStream();
try {
IOUtils.copy(is, System.err);
} finally {
is.close();
}
} else {
is = connection.getInputStream();
try {
IOUtils.copy(is, System.out);
} finally {
is.close();
}
}
}
private void setUpSSL(HttpsURLConnection connection) throws Exception {
byte[] bytes = FileUtils.readFileToByteArray(new File("d:\\workspace\\temp\\keystore"));
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(new ByteArrayInputStream(bytes), "changeit".toCharArray());
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("X509");
keyManagerFactory.init(keyStore, "changeit".toCharArray());
bytes = FileUtils.readFileToByteArray(new File("d:\\workspace\\temp\\truststore"));
KeyStore trustStore = KeyStore.getInstance("JKS");
trustStore.load(new ByteArrayInputStream(bytes), "changeit".toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("X509");
trustManagerFactory.init(trustStore);
SSLContext context = SSLContext.getInstance("TLS");
context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
SSLSocketFactory socketFactory = context.getSocketFactory();
connection.setSSLSocketFactory(socketFactory);
}
}
更新
我已经使用 .net WCF 客户端测试了该服务,并成功调用了该服务,所以我想知道问题出在哪里?为什么 WCF 客户端可以调用该服务,而即使使用普通的 HTTP POST 请求和 UrlConnection 的 Java 客户端却不能呢?
更新
这是一份 SOAP 消息样例:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<DoSomethingUseful xmlns="http://namespace/2012/01/service">
...
</DoSomethingUseful>
</soap:Body>
</soap:Envelope>
更新
我被告知在 .net 服务端会出现 "需要客户端证书。请求中未找到证书" 的信息,似乎是在 ssl 会话恢复时发生的。当 Content-Length 大于 48680 字节时才会出现该情况。 此外,WCF 服务配置了基本的 Http 绑定来使用传输层安全性。