My request executor class gives unidentified characters as response. please help me to sort out this
import java.net.HttpURLConnection;
connection = (HttpURLConnection) neturl.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Accept-Charset", "UTF-8");//ADDED
===here I do my some additions & logics ==
statusCode = connection.getResponseCode();
===here I do my some additions & logics.response code is 200 but response text not shows as correctly==
if ((statusCode == 200) || (statusCode == 201) || (statusCode == 302)) {
is = connection.getInputStream();
}
else {
is = connection.getErrorStream();
}
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String output;
while ((output = br.readLine()) != null) {
retStr += output;
}
this is my response [retStr]
}Umo�6�l���>� I�Y+��ك��b����D"U���/��;J�_�$�B�w���'7�����cHM�����v��,6��R�����/�����|�O���Ԙ��WU�UZ�=�Vg0�2�Y��jTkL����f<F�Q�I��%����,� *:F,��Q��T�K}�N���:gjp�6
��R�e�ca��2/2�D�Pq��m�G����a��H����P���9��T��~�^�'�Kdk�;��֠�[�&m��%fq�XR[)$łn�7\���Z.M�BµQ<*i�C�2#�(TL��!�;��h�)����n�G��h��s���z:� �tB��0����pr��#�E�P���m)�0G<"��5!˸��/yL��U�V+�F%�!(P�\�Tj"�P5��0c����-��î�j��±�Zr4���)�f��f�^Y��&��tT����X)��9݂lV]�������T1č��P5��,��&+�ũ�9.�.m�5Ǫ��
*��t���|r�M�rL f��c����u�xas�#g�:��On��PՁ�_�q���%a4�E��~$���a��s�щC%���"�kA�$ƍƋb��/!3������b����%c5����/����:�i���%�罺���o�貰��Q�s�6G�~o�5���]��o+��{��0��g輀}�V�ڸ�;�ﱙ#�7[�D�|�����L6�v���!�要ce�\��g�t�!�Ia�^��G�h�Hfk���Zr$R�j�Nu��[�\�u+��gr%�\�m�[N;���}�ӳ��2�l����ׯgKa�������ؼ"j�������xBpn6����+��/��G�L�F���R����yBf��N�����ڜZ�킏��i�J�%M�Cۉހ����n�;�G��7m���m;�����)�a�X��'�ћ�l#�GTi$���C��Vf���v��$�Z��93q����ߚ�H�Je�k�t�����+��?��ilb�RAu�.��* �9B^��(�)����
I think you are getting gzip stream as output here .. try below things in your code.
When you call https URL you get HttpsURLConnection as return which dont handle gzip streamsso try below.
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Accept-Encoding", "gzip");
...
InputStream inStream = new GZIPInputStream(conn.getInputStream());
Related
I brief description of what I'm doing, I'm automating creating incidents in ServiceNow using API. I have tested with POSTMAN first and it works fine.
Now after I wrote the code, I try to post data, I got no respond from the server. what could be the problem?
if (method.equals("POST") )
{
url+= "api/now/table/incident";
}
else if (method.equals("GET")|| method.equals("PUT"))
{
url+= "api/now/table/incident/" + incident.getSys_id();
}
URL request_url = new URL(url);
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
connection = (HttpsURLConnection) request_url.openConnection();
// ((HttpsURLConnection) connection).setSSLSocketFactory(sslsocketfactory);
if (method.equals("POST") || method.equals("PUT"))
{
connection.setRequestProperty("Content-Type", "application/json");
}
else
{
connection.setRequestProperty("Accept", "application/json");
}
connection.setRequestProperty("User-Agent", "Mozilla/5.0"); // this was suggested as solution, but doesn't work.
connection.setRequestMethod(method);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Authorization", "Basic "+encoded);
if (method.equals("POST") || method.equals("PUT"))
{
JSONObject json = new JSONObject();
json.put("short_description",incident.getShort_description());
json.put("description", incident.getDescription());
json.put("assignment_group",incident.getAssignment_group());
json.put("priority",incident.getPriority());
json.put("impact",incident.getImpact());
OutputStream os = connection.getOutputStream();
os.write(json.toJSONString().getBytes(StandardCharsets.UTF_8));
os.close();
}
// read the respond
httpsCode = connection.getResponseCode();
if (httpsCode == HttpsURLConnection.HTTP_OK || httpsCode == 201)
{
//Read
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
String lines = null;
StringBuilder stringBuilder = new StringBuilder();
while ((lines = bufferedReader.readLine()) != null)
{
stringBuilder.append(lines);
}
bufferedReader.close();
result = stringBuilder.toString();
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(result);
String incident_number = json.get("incident_number").toString();
String sys_id = json.get("sys_id").toString();
incident.setIncident_number(incident_number);
incident.setSys_id(sys_id);
return null;
}
the error is related to timeout, a posted solution suggested to set up the header to a browser type. but that's also didn't work.
I can read full response body (big JSON data, more than 400,000 chars) few times, but after 5-6 times my response is not full.
Here's my code of getting response:
URL address = new URL("https://myurl.com/");
HttpURLConnection Connection = (HttpURLConnection)address.openConnection();
Connection.setRequestMethod("GET");
Connection.setRequestProperty("accept-language", "en-US");
Connection.setRequestProperty("user-agent", UserAgent);
Connection.setRequestProperty("cookie", cookies);
Connection.setUseCaches(false);
Connection.setDoInput(true);
Connection.setDoOutput(true);
if (Connection.getResponseCode() == HttpsURLConnection.HTTP_OK)
{
String Response = new String();
InputStream is = Connection.getInputStream();
int ch;
StringBuffer sb = new StringBuffer();
while (( ch = is.read()) != -1) {
sb.append((char) ch);
}
Response = sb.toString();
is.close();
}
In my original code after is.close(), it is just lot of JSON parsing from Response string
I want to implement the code for handling POST requests using try with resources.
Following is my code:
public static String sendPostRequestDummy(String url, String queryString) {
log.info("Sending 'POST' request to URL : " + url);
log.info("Data : " + queryString);
BufferedReader in = null;
HttpURLConnection con = null;
StringBuilder response = new StringBuilder();
try{
URL obj = new URL(url);
con = (HttpURLConnection) obj.openConnection();
// add request header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Content-Type", "application/json");
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(queryString);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
log.info("Response Code : " + responseCode);
if (responseCode >= 400)
in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
else
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
}catch(Exception e){
log.error(e.getMessage(), e);
log.error("Error during posting request");
}
finally{
closeConnectionNoException(in,con);
}
return response.toString();
}
I have the following concerns for the code:
How to introduce conditional statements in try with resources for the above scenario?
Is there a way to pass on the connection in try with resources? (It can be done using nested try-catch blocks since URL and HTTPConnection is not AutoCloseable, which itself is not a compliant solution)
Is using try with resources for the above problem is a better approach?
Try this.
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
try (AutoCloseable conc = () -> con.disconnect()) {
// add request headers
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
wr.writeBytes(queryString);
}
int responseCode = con.getResponseCode();
try (InputStream ins = responseCode >= 400 ? con.getErrorStream() : con.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(ins))) {
// receive response
}
}
() -> con.disconnect() is a lambda expression which execute con.disconnect() at finally stage of the try statement.
1: You can use conditional statements inside try with resources statement also. Unfortunately you have to define new variable for this block and cannot use a predefined variable. ( variable in in your code)
try (BufferedReader in = (responseCode >= 400 ? new BufferedReader(new InputStreamReader(con.getErrorStream())) : new BufferedReader(new InputStreamReader(con.getInputStream())))) {
// your code for getting string data
}
2: I'm not sure HttpUrlConnection is AutoCloseable, So it might be a good idea to call the disconnect() yourself. I'm open to any suggestion on this one.
3: try with resources will definitely help you in managing the resources. But if you're confident that you're releasing the resources properly after use, then your code is fine.
I have generated the access code by using https://login.mailchimp.com/oauth2/authorize API. But when I try to create the token using https://login.mailchimp.com/oauth2/token, I'm getting unicode result like this.
(?M?? ?0F?UJ?N?NQ? %`??'
"?????nb??f=?&9????i'f??]?~j*$??W??Reg??_T1-???;?oc)
qryStr = {"client_secret":"**********","grant_type":"authorization_code","redirect_uri":"https%3A%2F%2Flocalhost%3A9443%2Fverifymailchimp.sas","client_id":"********","code":"*************"}
HttpURLConnection connection = null;
try
{
URL reqURL = new URL("https://login.mailchimp.com/oauth2/token");
connection = (HttpURLConnection) reqURL.openConnection();
connection.setConnectTimeout(3000); // 3 seconds
connection.setReadTimeout(5000); // 5 seconds
connection.setUseCaches(false);
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //No I18N
connection.setRequestProperty("Content-Length", "" + Integer.toString(qryStr.getBytes().length)); //No I18N
connection.setDoOutput(true);
OutputStream os = null;
try
{
os = connection.getOutputStream();
os.write(qryStr.getBytes(CHARSET));
}
finally
{
try{os.close();}catch(Exception e){}
}
int resCode = connection.getResponseCode();
boolean success = (resCode >= 200 && resCode < 300);
InputStream is = success ? connection.getInputStream() : connection.getErrorStream();
if (is == null)
{
return null;
}
String contentStr = null;
try
{
InputStreamReader reader = new InputStreamReader(is, CHARSET);
StringBuilder buffer = new StringBuilder();
char[] bytes = new char[1024];
int bytesRead;
while ((bytesRead = reader.read(bytes, 0, bytes.length)) > 0)
{
buffer.append(bytes, 0, bytesRead);
}
contentStr = buffer.toString();//?M?? ?0F?UJ?N?NQ? %`??' "?????nb??f=?&9????i'f??]?~j*$??W??Reg??_T1-???;?oc
}
finally
{
try{is.close();}catch(Exception e){}
}
}
Can anyone please tell the cause?
I found the cause of this case. An access code is valid for 30 seconds. Need to generate the token before the expiry. If they conveyed the proper error message, we can able to sort out the problem without any confusion :(
I am using Java.Net.URL for making a Rest webservice call.
using the below example code.
URL url = new URL("UrlToConnect");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
String input = "{\"qty\":100,\"name\":\"iPad 4\"}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
i am trying to capture response code from this webservice call. I observed that Even after putting a wrong URL i am getting 200 response code from the connection. Please suggest a way by which i can capture response codes 200 , 201 and 202.
Thanks.