gpt4 book ai didi

java:带有西里尔字符的 HttpURLConnection

转载 作者:太空宇宙 更新时间:2023-11-04 13:05:09 24 4
gpt4 key购买 nike

我需要向网络服务发出肥皂请求。只有 2 个函数,所以我决定使用简单的 HttpURLConnection 来加快开发速度。这是测试代码(不要害怕 try/catch,这只是一个测试)。

HttpURLConnection connection = null;
URL url = null;
try {
url = new URL("http://doc.ssau.ru/ssau_biblioteka_test/ws/DspaceIntegration.1cws");
} catch (MalformedURLException e) {
e.printStackTrace();
}

try {
connection = (HttpURLConnection)url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
connection.setRequestProperty("Authorization", "Basic d2Vic2VydmljZTp3ZWJzZXJ2aWNl");
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
connection.setRequestProperty("Accept-Encoding", "gzip,deflate");
try {
connection.setRequestMethod("POST");
} catch (ProtocolException e) {
e.printStackTrace();
}
connection.setDoOutput(true);

DataOutputStream wr = null;
try {
wr = new DataOutputStream(
connection.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}

String myString = "RU/НТБ СГАУ/WALL/Х62/С 232-948516";
byte bytes[] = new byte[0];
try {
bytes = myString.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String value = "";
try {
value = new String(bytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
wr.writeBytes("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:imc=\"http://imc.parus-s.ru\">\n" +
" <soap:Header/>\n" +
" <soap:Body>\n" +
" <imc:GetRecordsInfo>\n" +
" <imc:Codes>RU/НТБ СГАУ/WALL/Х62/С 232-948516</imc:Codes>\n" +
" <imc:Separator>?</imc:Separator>\n" +
" <imc:Type>?</imc:Type>\n" +
" </imc:GetRecordsInfo>\n" +
" </soap:Body>\n" +
"</soap:Envelope>");
} catch (IOException e) {
e.printStackTrace();
}
try {
wr.close();
} catch (IOException e) {
e.printStackTrace();
}

InputStream is = null;
try {
is = connection.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuilder response = new StringBuilder(); // or StringBuffer if not Java 5+
String line;
try {
while((line = rd.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
rd.close();
} catch (IOException e) {
e.printStackTrace();
}
}

所以问题是:当我使用西里尔字母进行 wirteByes 时,例如“RU/НТБ СГАУ/WALL/Х62/С 232-948516”,Web 服务返回 500。如果我仅使用拉丁语或留空,则一切正常。编码西里尔语的正确方法是什么?

更新问题解决了,使用了这个结构:

wr.write(new String("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:imc=\"http://imc.parus-s.ru\">\n" +
" <soap:Header/>\n" +
" <soap:Body>\n" +
" <imc:GetRecordsInfo>\n" +
" <imc:Codes>RU/НТБ СГАУ/WALL/Х62/С 232-948516</imc:Codes>\n" +
" <imc:Separator>?</imc:Separator>\n" +
" <imc:Type>?</imc:Type>\n" +
" </imc:GetRecordsInfo>\n" +
" </soap:Body>\n" +
"</soap:Envelope>").getBytes(charset));

最佳答案

不太确定可能出了什么问题,您可以尝试以下几种方法。

1)尝试不同的编码。以下代码可能会有所帮助。

Charset charset = Charset.forName("UTF-16");    
byte[] encodedBytes = myString.getBytes(charset);

2)您可以尝试的另一件事是猜测字符串的字符集。(请注意:不可能每次都 100% 正确地猜测字符集)

byte[] thisAppCanBreak = "this app can break"
.getBytes("ISO-8859-1");
CharsetDetector detector = new CharsetDetector();
detector.setText(thisAppCanBreak);
String tableTemplate = "%10s %10s %8s%n";
System.out.format(tableTemplate, "CONFIDENCE",
"CHARSET", "LANGUAGE");
for (CharsetMatch match : detector.detectAll()) {
System.out.format(tableTemplate, match
.getConfidence(), match.getName(), match
.getLanguage());
}

This link可能也会有帮助

关于java:带有西里尔字符的 HttpURLConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34545878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com