UTF-8 problem in response of SOAP request - java

When I make the SOAP request from SOAP UI it returns normal answer, but when I try from Java code it returns not understandable characters. I tried to convert answer to UTF8 format, but it did not help. Please advise a solution, may be something wrong with my SOAP request. Example of response: OÄžLU, bu it must be OĞLU or MÄ°KAYIL instead of MİKAYIL
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
String userCredentials = username + ":" + password;
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));
con.setRequestProperty("Authorization", basicAuth);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "text/xml");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(myXML);
wr.flush();
wr.close();
String responseStatus = con.getResponseMessage();
System.out.println(responseStatus);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String xmlResponse = response.toString();
I tried:
ByteBuffer buffer = ByteBuffer.wrap(xmlResponse.getBytes("UTF-8"));
String converted = new String(buffer.array(), "UTF-8");

Try this:
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));

The character encoding is set as part of the Content-Type header.
I believe you're accidentally mixing charsets, which is why it is not displaying properly.
Try adding the charset to Content-Type like so:
con.setRequestProperty("Content-Type", "text/xml; charset=utf-8");

Would you try this?
con.setRequestProperty("Content-Type", "text/xml; charset=utf-8");

Related

How to use Satang "orders/user" API with java?

I've built java code to use satang "orders/user" api.
But got 405 error.
Please help me.
I've built placeLimitOrder() function.
And used successfully.
Then getUserOrders() function also made with same rult.
But this function got 405 error.
I can't find the reason.
public String getUserOrders (String pair,String limit,String offset,String status,String side)
{
String req="limit="+limit+"&offset="+offset+"&pair="+pair+"&side="+side+"&status="+status;
String operation="orders/user";
String signature=getSignature(req);
URL url = new URL(baseUrl+operation);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput( true );
con.setInstanceFollowRedirects( false );
con.setRequestProperty("Authorization", "TDAX-API "+this.key);
con.setRequestProperty("Signature",signature);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("charset", "utf-8");
con.setRequestProperty("User-Agent", "java client");
con.setUseCaches( false );
JsonObject obj=new JsonObject();
obj.addProperty("limit", limit);
obj.addProperty("offset", offset);
obj.addProperty("pair", pair);
obj.addProperty("side", side);
obj.addProperty("status", status);
String json=obj.toString();
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(json);
wr.flush();
wr.close();
int responseCode=con.getResponseCode();
if(responseCode!=HttpURLConnection.HTTP_OK){
throw new BadResponseException(responseCode);
}
BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder result = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
return result.toString();
}
Following code working well.
I can't find the reason.
public String placeLimitOrder(String amount,String pair,String price,String side) throws IOException, BadResponseException
{
Long lnonce=new Date().getTime();
String nonce=lnonce.toString();
String req="amount="+amount+"&nonce="+nonce+"&pair="+pair+"&price="+price+"&side="+side+"&type=limit";
String operation="orders/";
String signature=getSignature(req);
URL url = new URL(baseUrl+operation);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setDoOutput( true );
con.setInstanceFollowRedirects( false );
con.setRequestProperty("Authorization", "TDAX-API "+this.key);
con.setRequestProperty("Signature",signature);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("charset", "utf-8");
con.setRequestProperty("User-Agent", "java client");
con.setUseCaches( false );
JsonObject obj=new JsonObject();
obj.addProperty("amount", amount);
obj.addProperty("nonce", nonce);
obj.addProperty("pair", pair);
obj.addProperty("price", price);
obj.addProperty("side", side);
obj.addProperty("type", "limit");
String json=obj.toString();
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(json);
wr.flush();
wr.close();
int responseCode=con.getResponseCode();
if(responseCode!=HttpURLConnection.HTTP_OK){
throw new BadResponseException(responseCode);
}
BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder result = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
return result.toString();
}
public String getUserOrders(String pair,String limit,String offset,String status,String side) throws IOException, BadResponseException
{
String req="limit="+limit+"&offset="+offset+"&pair="+pair+"&side="+side+"&status="+status;
String operation="orders/user?"+req;
String signature=getSignature("");
URL url = new URL(baseUrl+operation);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setDoOutput( true );
con.setInstanceFollowRedirects( false );
con.setRequestProperty("Authorization", "TDAX-API "+this.key);
con.setRequestProperty("Signature",signature);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("charset", "utf-8");
con.setRequestProperty("User-Agent", "java client");
con.setUseCaches( false );
int responseCode=con.getResponseCode();
if(responseCode!=HttpURLConnection.HTTP_OK){
throw new BadResponseException(responseCode);
}
BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder result = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
return result.toString();
}
So if you are trying to do GET to https://api.tdax.com/api/orders/user
Headers should include:
Signature
Authentication
Query Parameters:
pair
limit
offset
status
side
From your code, it looks like you have put everything to the Signature.
Link to API docs
This is what you should fix:
URL. It should look like:
https://api.tdax.com/api/orders/user?pair=pairValue&limit=limitValue&offset=... add all the parameters and insert values.
Change request method to GET
Double-check your headers (Signature, Authentication).
Error code 405 - "bad request", which means most likely your request URL is bad. After you will fix URL, you should be able to get a different response code like 403/401 which should indicate that something is wrong with signature or authentication. In the best-case scenario, you will get 200 and then try to parse the JSON response.
Also, I don't recommend you to use HttpURLConnection, Generally, it’s NOT recommended to use this class, because the codebase is very old and outdated, it may not supports the new HTTP/2 standard, in fact, it’s really difficult to configure and use this class. Please consider using OkHttp, Apache HttpClient or other libraries.

I need to send a POST request to an api using an XML request structure in java

String urly = "myurl";
URL url = new URL(myurl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type","application/xml");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(xml);
wr.flush();
i am not sure whether this sends the request using xml request structure which is stored in String "xml". I dont know any other way to send request using XML.
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader iny = new BufferedReader(new InputStreamReader(con.getInputStream()));
String output;
StringBuffer res = new StringBuffer();
while ((output = iny.readLine()) != null) {
res.append(output);
}
iny.close();
wr.close();
//printing result from response
System.out.println(res.toString());
The response i am getting shows Invalid Request.
The generated XML was wrong, it needed to be checked.

Server returned HTTP response code: 405

I need to fetch content from third party web application UI but after login, site redirects to many pages. I am not understanding how to retrieve data from last opened page.
Presently I am receiving in.readLine() method returns null if I use "OPTIONS" instead of GET.
If I use GET then error 405.
Rest Client shows the connection success through GET method and also redirecting it to desired page.
Please suggest.
I connected to url through URLConnection
HttpsURLConnection con = (HttpsURLConnection) new URL("https://***.com/MRcgi/MRhomepage.pl" + "?" + query).openConnection();
Complete code is as follows-
String charset = "UTF-8"; // Or in Java 7 and later, use the constant:
String USER = "*****";
String PROJECTID = "1";
String MRP = "1eba539717f66151f557b49fd7e8a8d28";//dynamically changes
String OPTION = "none";
String WRITECACHE = "1";
String FIRST_TIME_IN_FP = "1";
String FIRST_TIME_IN_PROJ = "1";
String dispatch_script = "MRlogin.pl";
String query = String
.format("USER=%s&PROJECTID=%s&MRP=%s&OPTION=%s&WRITECACHE=%s&FIRST_TIME_IN_FP=%s&FIRST_TIME_IN_PROJ=%s&dispatch_script=%s&",
URLEncoder.encode(USER, charset),
URLEncoder.encode(PROJECTID, charset),
URLEncoder.encode(MRP, charset),
URLEncoder.encode(OPTION, charset),
URLEncoder.encode(WRITECACHE, charset),
URLEncoder.encode(FIRST_TIME_IN_FP, charset),
URLEncoder.encode(FIRST_TIME_IN_PROJ, charset),
URLEncoder.encode(dispatch_script, charset));
HttpsURLConnection con = (HttpsURLConnection) new URL(
"https://***com/MRcgi/MRhomepage.pl" + "?" + query)
.openConnection();
String userPassword = "domain\\user:password";
String encoding = new String(
org.apache.commons.codec.binary.Base64
.encodeBase64(org.apache.commons.codec.binary.StringUtils
.getBytesUtf8(userPassword)));
System.out.println("----" + encoding);
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type", "text/plain");
con.setRequestProperty("charset", "UTF-8");
con.setRequestProperty("Authorization", "Basic " + encoding);
USER=user&MRP=15c6ca083c2f75a73e0fbbd2832290f29&PROJECTID=1&USECACHEURL=1&IGNORE_REAL_ACTIVE_TIME=1";
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
//wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
System.out.println("-----" + in.readLine());
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}
setDoOutput(true) is used for POST and PUT requests. If it is false then it is for using GET requests.
Its not required in my code, so I simply commented setDoOutput(true), and request went as GET
Also below url helped me a lot in understanding it-
What exactly does URLConnection.setDoOutput() affect?

IBM Watson Q and A API gives 500 response code

I have written a Java Code to test the Watson Question and Answers API. However, I'm getting response code 500, when I run it. I have checked the api url and my login credentials. The problem seems to be somewhere else. Any hints or debugging suggestions would be of great help.
String url = "https://watson-wdc01.ihost.com/instance/526/deepqa/v1/question";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
String userCredentials = "username:password";
String basicAuth = "Basic " + DatatypeConverter.printBase64Binary(userCredentials.getBytes());
con.setRequestProperty ("Authorization", basicAuth);
con.setRequestProperty("X-SyncTimeout", "30");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Cache-Control", "no-cache");
con.setRequestMethod("POST");
String query = "{\"question\": {\"questionText\": \"" + "What are the common respiratory diseases?" + "\"}}";
System.out.println(query);
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(query);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
When i open the same url with the browser I get this:
Watson Error!!
Error Encountered!!
Unable to communicate with Watson.
Whats wrong? Could it be something with the configuration? Or is the server down?
Any hints or debugging suggestions would be of great help.
Attempt the same request using your web browser ... or the curl utility.
Capture and output the contents of the error stream.
I don't think that this is the cause of your problems, but it is wrong anyway:
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(query);
wr.flush();
You are writing to an API that expects text (JSON). You should therefore use a Writer, not a data (binary) output stream:
Writer wr = new OutputStreamWriter(con.getOutputStream(), "LATIN-1");
wr.write(query);
wr.flush();

Posting to a user's wall using the POST request and graph API

I am new to facebook app development and i have been trying to post a simple message on the wall of the user.i have managed to get the access token .Here is the code for the POST request.I am using java servlets
String data = URLEncoder.encode("access_token", "UTF-8") + "=" + URLEncoder.encode(accessToken, "UTF-8");
data += "&" + URLEncoder.encode("message", "UTF-8") + "=" + URLEncoder.encode("finally", "UTF-8");
out.println("data is\n"+data);
// Send data
String u="https://graph.facebook.com/me/feed";
URL urls = new URL(u);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
Well this code is not working and i can not post on the wall.Any suggestion as to where i might be wrong?
I'm pretty sure that it's because you don't specify the application/x-www-form-urlencoded content type, try this:
URLConnection connection = new URL("https://graph.facebook.com/me/feed").openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(data);
out.flush();
out.close()
Edit
Ok, so there are two more things that might cause this problem:
You also need to specify the content length.
You might need to read the response to make it count..
This code was tested and it works:
StringBuffer buffer = new StringBuffer();
buffer.append("access_token").append('=').append(ACCESS_TOKEN);
buffer.append('&').append("message=").append('=').append("YO!");
String content = buffer.toString();
URLConnection connection = new URL("https://graph.facebook.com/me/feed").openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", Integer.toString(content.length()));
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(content);
out.flush();
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();

Categories

Resources