0
我有一个可用的XML POST请求,直到我将字段更改为包含拉丁UTF-8字符(如“Δ)。我从该服务收到400条差评。拉丁字符破解Java中的XML发布请求
这两个请求都可以在Chrome浏览器扩展邮递员中使用。
我假设这与Java编码字符或读取数据流的方式有关。以下是我的代码,包括相关的库。如何解决这个问题?谢谢!
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import net.valutec.ws.*;
public class CardCallbacks extends ValutecWSCallbackHandler {
private void sendInfoToMaropost(GiftCard please) throws IOException {
URL serverUrl = new URL(TARGET_URL + API_KEY);
URLConnection urlConnection = serverUrl.openConnection();
HttpURLConnection hcon = (HttpURLConnection)urlConnection;
System.out.println(dont.getBarcode());
try {
hcon.setReadTimeout(10000);
hcon.setConnectTimeout(15000);
hcon.setRequestMethod("POST");
hcon.setRequestProperty("Content-Type", "application/xml");
hcon.setRequestProperty("Accept", "application/xml");
hcon.setDoInput(true);
hcon.setDoOutput(true);
String body =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<record>" +
" <orderlineid>fakeorderid</orderlineid>" +
" <encoded-barcode>Î</encoded-barcode>" +
//the request that doesn't work
"</record>";
OutputStream output = new BufferedOutputStream(hcon.getOutputStream());
output.write(body.getBytes());
output.flush();
int responseCode = hcon.getResponseCode();
System.out.println(responseCode);
if(responseCode == 200) {
}
BufferedReader in = new BufferedReader(
new InputStreamReader(hcon.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
System.out.println(response);
in.close();
}
finally {
hcon.disconnect();
}
}
编辑:我找到了解决方案。这有助于:Unicode Characters 这是需要编辑:
output.write(body.getBytes("UTF-8"));