Exisitng code :
Sending id and pass in url and xml data in body
String url = http://localhost:8080/FirstServlet/MyFirstServletMapping&id=123&pass=sDff
HttpURLConnection urlc = (HttpURLConnection) new URL(url).openConnection();
urlc.setDoOutput(true);
urlc.setRequestMethod("POST");
OutputStream out = urlc.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write("<xml>");
writer.flush();
writer.close();
Now I need to change the code to send id and pass also in POST.
How can i change the code to add id and pass in POST and do not break the server read.
I tried as below but am seeing xml is getting appended at end of pass.
Will this cause any issue at server side? Or this looks fine?..Any suggestions please?
New Code:
String url = "http://localhost:8080/FirstServlet/MyFirstServletMapping";
HttpURLConnection urlc = (HttpURLConnection) new URL(url).openConnection();
urlc.setDoOutput(true);
urlc.setRequestMethod("POST");
OutputStream out = urlc.getOutputStream();
byte[] postData = setRequestData(111124l, "password5");
out.write(postData);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write("xml");
writer.flush();
writer.close();
private static byte[] setRequestData(Long prsId, String password) throws IOException {
Map<String, Object> params = new LinkedHashMap<String, Object>();
params.put("id", prsId);
params.put("pass", password);
StringBuilder postData = new StringBuilder();
for (Map.Entry<String, Object> param : params.entrySet()) {
if (postData.length() != 0)
postData.append('&');
try {
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('='); postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
} catch (UnsupportedEncodingException e) {
}
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
System.out.println("post data is--->"+postData.toString());
return postDataBytes;
}
Output:
in post method
dsid-->111124
password is-->password5xml
Served at: /FirstServlet: end
Related
I am trying to call an API that gives 400 on some requests.
But it has meaningful message that needs to be read.
Also i am passing a payload(json body) in the api call
My code(it takes a payload as json) and the 400 response is below
I am able to successfully read the response of 200 but issue is with 400
public static void blahblah (String input, String CustomerID, String endpoint, String dateNameFolder) throws UnknownHostException
{
try {
URL url = new URL(endpoint);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
StringBuilder sb = new StringBuilder();
String output;
while ((output = br.readLine()) != null) {
sb.append(output);
}
File fileObj = new File(CustomerID + ".json");
if (!fileObj.exists()) {
fileObj.createNewFile();
FileWriter dataWriter = new FileWriter(CustomerID + ".json");
dataWriter.write(sb.toString());
dataWriter.close();
} else {
FileWriter dataWriter = new FileWriter(CustomerID + ".json");
dataWriter.write(sb.toString());
dataWriter.close();
}
conn.disconnect();
}catch (MalformedURLException e) {
l
log.info(e);
}
catch (IOException e) {
log.error("Error in Validating Request to Catalog for Customer ID(400 Bad Request) : " + CustomerID ) ;
log.info(e);
}
}
Sample output of 400 bad request :
<Response>
<StatusCode>BadRequest</StatusCode>
<ErrorCode>OrderRequestInvalid.InvalidCharacteristicUse</ErrorCode>
<Message>Characteristic ID cannot be found in the specification: { EntityUniqueCode: CS_0aefdfec-022e-4ac9-a84b-83c8ea2e0fd0, CharacteristicID: 76c4e767-ce7b-4675-a178-d832839b322f}</Message>
<Context>
<EntityUniqueCode>CS_0aefdfec-022e-4ac9-a84b-83c8ea2e0fd0</EntityUniqueCode>
<CharacteristicID>76c4e767-ce7b-4675-a178-d832839b322f</CharacteristicID>
</Context>
</Response>
You need to choose correct stream depending on your response status. here is an example how you can do this:
BufferedReader br = null;
int statusCode = conn.getResponseCode();
if (statusCode>299){
br = new BufferedReared(new InputStreamReader(conn.getErrorStream()));
} else {
br = new BufferedReared(new InputStreamReader(conn.getInputStream()));
}
I am hitting below url using any rest client and I get the api Response :400 Bad Request with response body
INPUT param
POST http://SOME.IP:8008/equipment_api/F0-03-8C-C3-D3-CC/832
HEADERS
Content-Type application/json
X-RequestID 1234
BODY
{
"items":[{"updateValue":1, "updateKey": "RESETDEV"}],
"sync":"false"
}
Response :400 Bad Request
{
"error": "RESETDEV is not a valid key."
}
But java simple client does not show the response body.. below is java code.. it just give 400 bad req.
public static void main(String[] args) {
try {
String urlParameters = " {\r\n" +
"\"items\":[{\"updateValue\":\"Hi\", \"updateKey\": \"RESETDEV\"}],\r\n" +
"\"sync\":false\r\n" +
"}";
URL url = new URL("http://SOME.IP:8008/equipment_api/F0-03-8C-C3-D3-CC/832");
URLConnection conn = url.openConnection();
conn.setRequestProperty("content-type", "application/json");
conn.setRequestProperty("X-RequestID", "1234");
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(urlParameters);
writer.flush();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
writer.close();
reader.close();
}catch(Exception ex) {
System.out.println("some error :: "+ex.toString());
}
}
Try to use following code
final String uri = "http://SOME.IP:8008/equipment_api/F0-03-8C-C3-D3-CC/832";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>("X-RequestID", "1234");
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
System.out.println(result);
Wrap with Try Catch.
May it is useful to you. It will work
public static void main(String[] args) {
try {
String urlParameters = " {\r\n" +
"\"items\":[{\"updateValue\":\"Hi\", \"updateKey\":
\"RESETDEV\"}],\r\n" +
"\"sync\":false\r\n" +
"}";
URL url = new URL("http:http://SOME.IP:8008/equipment_api/F0-03-
8C-C3-D3-CC/832");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("content-type", "application/json");
con.setRequestProperty("X-RequestID", "1234");
con.setDoOutput(true);
OutputStreamWriter writer = new
OutputStreamWriter(con.getOutputStream());
writer.write(urlParameters);
writer.flush();
String line;
//BufferedReader reader = new BufferedReader(new
InputStreamReader(con.getInputStream()));
InputStream is = con.getInputStream();
if(con.getResponseCode() >= 200 && 299 <= con.getResponseCode()) {
is = con.getInputStream();
}else {
is = con.getErrorStream();
}
try (BufferedReader br = new BufferedReader(new InputStreamReader(is,
"utf-8"))) {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
writer.close();
// br.close();
}catch(Exception ex) {
System.out.println("some error :: "+ex.toString());
}
}
I'm try to pass 19(images) parameters to htttp post request, but when I put more then 9 params, my app don't do the upload to server, I recheck all the code and is ok, server side is ok also.
#Override
protected String doInBackground(Void... params) {
Log.d("SETIMAGE", "IMAGEM DOINBACKGROUND INIT");
RequestHandler rh = new RequestHandler();
HashMap<String,String> param = new HashMap<String,String>();
param.put(KEY_ID,id);
param.put(KEY_CHAMADO,chamado);
param.put(FACHADA,imageFachada);
param.put(RADIO,imageRadio);
param.put(SUPORTE,imageSuporte);
param.put(MASTRO,imageMastro);
param.put(ISOLAMENTO,imageIsolamento);
param.put(INFRAEXT1,imageInfraExt1);
param.put(INFRAEXT2,imageInfraExt2);
param.put(INFRAINT1,imageInfraInt1);
param.put(INFRAINT2,imageInfraInt2);
param.put(CONECTOREXT,imageConectorExt);
param.put(CONECTORINT,imageConectorInt);
param.put(SALATEC,imageSalaTec);
param.put(RACK,imageRack);
param.put(IDU,imageIDU);
param.put(TOMADAIDU,imageTomadaIDU);
param.put(AZIMUTE,imageAzimute);
param.put(GPS,imageGPS);
param.put(MTCABOEXT,imageMtCaboExt);
param.put(MTCABOINT,imageMtCaboInt);
String result = rh.sendPostRequest(UploadUrl, param);
Log.d("RESULT", result);
return result;
}
This is via RequestHandler.class
public String sendPostRequest(String requestURL, HashMap<String, String> postDataParams) {
//Creating a URL
URL url;
//StringBuilder object to store the message retrieved from the server
StringBuilder sb = new StringBuilder();
try {
//Initializing Url
url = new URL(requestURL);
//Creating an httmlurl connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//Configuring connection properties
conn.setReadTimeout(150000);
conn.setConnectTimeout(150000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
//Creating an output stream
OutputStream os = conn.getOutputStream();
//Writing parameters to the request
//We are using a method getPostDataString which is defined below
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
sb = new StringBuilder();
String response;
//Reading server response
while ((response = br.readLine()) != null) {
sb.append(response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
I need to pass all images to upload to server. But it's ok only when I pass 9 param.put
You should consider using Retrofit, which allows to upload huge size files in chunks and has more flexibility than most libraries.
Sorry to ask you this but I am kinda new in Java.
I have the following classes:
protected class LoginMethod{
public LoginMethod() throws MalformedURLException, UnsupportedEncodingException, IOException{
URL url = new URL("http://example.com/login");
Map<String,Object> params = new LinkedHashMap<>();
params.put("username", user);
params.put("password", password );
StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
try{
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
for ( int c = in.read(); c != -1; c = in.read() )
token += String.valueOf((char)c);
System.out.println(token);
new LoggedIn();
}catch(IOException es){
System.out.println(es);
}
}
}
And this returns a JSON String with some token.
And the next class :
protected class DoStuff{
public DoStuff() throws MalformedURLException, UnsupportedEncodingException, IOException, ParseException{
JSONParser parser = new JSONParser();
URL url = new URL("http://example.com/DoStuff");
Map<String,Object> params = new LinkedHashMap<>();
params.put("Do", "Stuff");
StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
try{
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
for ( int c = in.read(); c != -1; c = in.read() )
stuff +=(char)c;
System.out.println(stuff);
new LogOut();
}catch(IOException er){
System.out.println(er);
}
}
}
This should return the DoStuff thing. But because I don't know how to keep session between those classes I get 401 error.
My question is how to keep the session between those classes or a tip how to put them into a single class that supports session keeping in mind they are called by separate buttons.
Thank you!
If the mentioned "http://example.com" is a webservice, the token which you received in the response is having session information. All you need to be do is to send the token back by setting in request header while calling dostuff method.
login response :
{ "token":"ewryoiuasdjhie8417098412","expires":"jun 10 2015"}
while calling dostuff
conn.setRequestProperty("token", "ewryoiuasdjhie8417098412");
The proper answer is the following:
In the second class we need to json decode the token received and add
conn.setRequestProperty("Authorization", token);
This works. Thanks StackOverflow for everything.
I have method to call web service of signin and it is working fine when call it from main() method ... but when call it from event handler of button on JFrame I got : Server returned HTTP response code: 500 for URL: http://localhost/postrec/index.php/api/Site/Login Error.
this is happen at the line code :Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
the Method is :
public boolean doLogin(String username, String password) throws MalformedURLException{
try{
URL url = new URL("http://localhost/postrec/index.php/api/Site/Login");
Map<String,Object> params = new LinkedHashMap<>();
params.put("username", username);
params.put("password", password);
StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String responseStr = "";
for (int c; (c = in.read()) >= 0; responseStr = responseStr + (char)c);
JSONObject jsonObject = new JSONObject(responseStr);
return (jsonObject.getString("code").equals("200"));
}catch(JSONException | MalformedURLException | UnsupportedEncodingException e){
throw new MalformedURLException(e.getMessage());
}catch(IOException e){
System.out.println(e.getMessage());
}
return false;
}