Java HTTPS调用提供错误消息,而SOAPUI提供成功响应

时间:2014-07-12 16:03:49

标签: java

我使用以下代码调用post方法类型的rest服务。 java代码返回错误消息。当我使用soapUI和post query string复选框调用相同的服务时,我得到一个有效的响应。如果我遗漏了以下代码中的任何内容,请告诉我。

String data = null;
StringBuilder stringbuilder = null;
url1 = new URL(URL);
conn = (HttpsURLConnection) url1.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Host",url1.getHost());
//conn.setRequestProperty("Host","host.info");
conn.setRequestProperty("Accept", "application/json");
conn.setConnectTimeout(5000);
String userPassword = userName + ":" + password;
String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes());
// set up the HttpClient to do basic/digest authentication.
conn.setRequestProperty("Authorization", "Basic " + encoding);
//post data test block starts
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Contentlength",Integer.toString(parameters.getBytes().length));
conn.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(parameters);
wr.close ();
//post data test block ends
InputStream content = null;
if(conn.getResponseCode() == 200) {
content = (InputStream) conn.getInputStream();
} else {
content = (InputStream) conn.getErrorStream();
}
BufferedReader rd = new BufferedReader(new InputStreamReader(content));
stringbuilder = new StringBuilder();
string line;
while ((line = rd.readLine()) != null) {
stringbuilder.append(line);
}

0 个答案:

没有答案