I need to post data to server using HttpURLConnection. Data contains Thai character as well. Server which accept post request accept encoding UTF-8 and TIS-620 both. When I directly post data from rest client it works fine, but when I send same request from java code it is not working properly, I mean when I send UTF-8 format data it gives parse exception and when I use TIS-620 instead thai text in server I am getting some speial character square etc.( I do not any have handle on server which accept data )
I am setting same header property for HttpURLConnection which I set for rest client in browser.Please let me know what could be going wrong here
As per my requirement I have to write this code in servlet , and servlet is called from browser AJAX call. In JQUERY AJAX call while sending data I am setting
beforeSend: function(xhr) {
xhr.setRequestHeader( "Content-type", "application/json; charset=UTF-8" );
},
I changed UTF-8 to TIS-620 All places but no luck
Some finding :
when I set
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
and Print connection.getContentType() it is only printing application/json
If I send only English text it works fine. I am giving servlet code below which accept post request and POST it to server
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
System.err.println("************** POST CALLED **************");
//PRINT SERVLET REQUEST PROPERTY
System.err.println("CharacterEncoding : "+ request.getCharacterEncoding());
System.err.println("ContentType : "+request.getContentType());
//SEND GET REQUEST AND FETCH XCSRF TOKEN
String dummyServiceUrl = "GET_LOT_SRV/get_lot";
String xcsrfToken = null;
HttpURLConnection connection = null;
String requestURL = httpPrefix + hostName + semiColon + portNumber + forwardSlash + dummyServiceUrl;
List<String> cookies = null;
try {
URL gatewayServiceUrl = new URL(requestURL);
connection = (HttpURLConnection) gatewayServiceUrl.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Authorization", this.getBasicAuth());
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setRequestProperty("x-csrf-token", "fetch");
connection.connect();
if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
//ON SUCCESS GET XCSRF TOKEN AND IN SAME SESSION POST DATA
requestURL = httpPrefix + hostName + semiColon + portNumber + forwardSlash + request.getQueryString();
gatewayServiceUrl = new URL(requestURL);
connection = (HttpURLConnection) gatewayServiceUrl.openConnection();
//SET CONNECTION PROPERTY
connection.setRequestMethod("POST");
xcsrfToken = connection.getHeaderField("x-csrf-token");
cookies = connection.getHeaderFields().get("set-cookie");
// SET COOKIES
for (String cookie : cookies) {
String tmp = cookie.split(";", 2)[0];
connection.addRequestProperty("Cookie", tmp);
}
//SET HEADERS
connection.setRequestProperty("Authorization", this.getBasicAuth());
connection.setRequestProperty("x-csrf-token", xcsrfToken);
connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("DataServiceVersion", "2.0");
connection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
//SET USERS INPUT DATA TO OUTPUT STREAM
String payload = this.getDataFromStreamPost(request.getInputStream());
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.write(payload.getBytes());
dataOutputStream.flush();
dataOutputStream.close();
//POST DATA AND CHECK RESPONSE
connection.connect();
response.setStatus(HttpURLConnection.HTTP_CREATED);
response.setContentType("application/json; charset=TIS-620");
response.getWriter().println(this.getDataFromStream(connection.getInputStream()));
} else {
System.err.println("XCSRF GET FAILURE "+connection.getResponseCode());
response.setStatus(connection.getResponseCode());
response.setContentType("application/json; charset=TIS-620");
response.getWriter().println(this.getDataFromStream(connection.getInputStream()));
}
} catch (Exception e) {
System.err.println("EXCEPTION OCCURED IN POST : "+e.getMessage());
response.setStatus(connection.getResponseCode());
response.setContentType("application/json; charset=TIS-620");
response.getWriter().println(this.getDataFromStream(connection.getErrorStream()));
}
}
private String getBasicAuth() {
String userpass = userName + ":" + password;
return "Basic "
+ javax.xml.bind.DatatypeConverter.printBase64Binary(userpass
.getBytes());
}
private String getDataFromStream(InputStream stream) throws IOException {
StringBuffer dataBuffer = new StringBuffer();
BufferedReader inStream = new BufferedReader(new InputStreamReader(
stream));
String data = "";
while ((data = inStream.readLine()) != null) {
dataBuffer.append(data);
}
inStream.close();
return dataBuffer.toString();
}
private String getDataFromStreamPost(InputStream stream) throws IOException {
StringBuffer dataBuffer = new StringBuffer();
BufferedReader inStream = new BufferedReader(new InputStreamReader(
stream,"UTF-8"));
String data = "";
while ((data = inStream.readLine()) != null) {
dataBuffer.append(data);
}
inStream.close();
return dataBuffer.toString();
}
Related
I am not able to read multipart file from the http response. The response contains the MIME Boundary and content transfer encoding - binary. we need to read the date from the response and send the request to another http post method. here we are facing two issues.
Not able to read the binary file properly from the http response.
Not able to form the multipart file to send the request.
when I have send that multipart form data in http post method. I am not receiving proper response. beacuse of the multipart binary encoding file. please provide the sample to read the multipart binary file and how to form and send the multipart file.
private void postLocalRequest(URL url, byte[] requestBody, String contentType, String authHeader)
throws IOException, ProtocolException, Exception {
logger.info("local request URL:::" + url);
logger.info("local request content type:::" + contentType);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
if (!authHeader.isEmpty()) {
httpURLConnection.setRequestProperty("Authorization", authHeader);
}
httpURLConnection.addRequestProperty("Accept", "application/json; charset=UTF-8");
httpURLConnection.setRequestProperty("Content-Type", contentType);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
outputStream.write(requestBody);
outputStream.flush();
logger.info("waiting for local response");
BufferedReader bufferedReader = null;
logger.info("http response code :::" + httpURLConnection.getResponseCode());
deviceResponse = "";
deviceResponseContentType = httpURLConnection.getContentType();
logger.info("device Response Content Type::" + deviceResponseContentType);
if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
deviceResponse = bufferedReader.lines().collect(Collectors.joining("\n"));
XWCConnectorSupplementalLogging.logXML(logger, "plr", "Device Request Output", deviceResponse);
} else {
try {
InputStream ip = httpURLConnection.getInputStream();
String info = new BufferedReader(new InputStreamReader(ip)).lines().collect(Collectors.joining("\n"));
deviceResponse = info;
XWCConnectorSupplementalLogging.logXML(logger, "plr", "Device Request Output", info);
} catch (Exception e) {
try {
InputStream es = httpURLConnection.getErrorStream();
String error = new BufferedReader(new InputStreamReader(es)).lines()
.collect(Collectors.joining("\n"));
if(httpURLConnection.getResponseCode() != HttpURLConnection.HTTP_NOT_FOUND) {
deviceResponse = error;
}
XWCConnectorSupplementalLogging.logXML(logger, "plr", "Device Request Error", error);
} catch (Exception e1) {
logger.severe("Unable to read Request error or output");
}
}
}
}
I'm getting a 'Server returned HTTP response code: 500' error although I have checked what I'm sending (I even tried sending it with an online tool and it worked). The API Key and the JSON are correct. I get this error when trying to read the input stream with 'connection.getInputStream()'. Where could this be comming frome ? Did I forget something ? I am trying to implement this feature from the openrouteservice API : https://openrouteservice.org/dev/#/api-docs/v2/directions/{profile}/post
public static UPSRoute getRoute(Location start, Location end, String language) {
if (language.equals("fr")) {
JSONObject jsonObject = null;
try {
URL url = new URL("https://api.openrouteservice.org/v2/directions/foot-walking");
String payload = "{\"coordinates\":[[" + start.getCoordinates() + "],[" + end.getCoordinates() + "]],\"language\":\"fr\"}";
System.out.println(payload); //{"coordinates":[[1.463478,43.562038],[1.471717,43.560787]],"language":"fr"}
byte[] postData = payload.getBytes(StandardCharsets.UTF_8);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", API_KEY);
connection.setRequestProperty("Accept", "application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8");
connection.setDoOutput(true);
try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
wr.write(postData);
}
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); // Error is right here
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
connection.disconnect();
jsonObject = new JSONObject(content.toString());
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return new UPSRoute(jsonObject);
} else {
return getRoute(start, end);
}
}
Here is the error :
java.io.IOException: Server returned HTTP response code: 500 for URL: https://api.openrouteservice.org/v2/directions/foot-walking/json
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1913)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1509)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:245)
at UPSRouteService.getRoute(UPSRouteService.java:63)
at Main.main(Main.java:5)
Thanks to Andreas, it was just missing the line :
connection.setRequestProperty("Content-Type", "application/json");
It works fine now.
I am trying to make a GET request to a local server I have running. I am having trouble returning the correct data, I am seeing an 'Unauthorized' response. Can anyone spot any glaring issues with this given that the String 'token' is correct.
protected Object doInBackground(Void... params) {
try {
String url = "http://192.168.0.59:8000/events/";
URL object = new URL(url);
HttpURLConnection con = (HttpURLConnection) object.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Authorization:", "Token " + token);
//Display what the GET request returns
StringBuilder sb = new StringBuilder();
int HttpResult = con.getResponseCode();
if (HttpResult == HttpURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(
new InputStreamReader(con.getInputStream(), "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
} else {
System.out.println(con.getResponseMessage());
}
} catch (Exception e) {
Log.d("Uh Oh","Check your network.");
return false;
}
return false;
}*
I was able to get a curl request working from the command line:
curl -H "Authorization: Token token" http://0.0.0.0:8000/events/
try this
con.setRequestProperty("Authorization", "Bearer " + token);
It turns out this issue was caused by including the con.setDoOutput(true); as get requests do not include a body.
In my project I must strictly use HttpURLConnection class
I have this following code which I got from the internet
MultipartEntity multiPart = new MultiPartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null Chartset.forName("UTF-8");
File f = new File("/home/abhishek/foo.docx");
FileBody fb = new FileBody(f);
multiPart.addPart("file", fb);
HttpPost post = new HttpPost();
post.setHeader("ENCTYPE", "multipart/form-data");
post.setEntity(multiPart);
Problem is that I cannot use HttpPost ... In my project only HttpURLConnection class works!
So I need to translate the code above into HttpURLConnection.
I cannot find anything similar to setEntity on the HttpUrlConnection.
Edit::
Based on the suggestions below. I have this code
public class RESTFileUpload {
public static void main(String[] args) throws Exception {
Authenticator.setDefault(new Authenticator() {
#Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("domain\\user", "Password".toCharArray());
}
});
String filePath = "/home/abhishek/Documents/HelloWorld.docx";
String fileName = "HelloWorld.docx";
String fileNameShort = "HelloWorld";
String urlStr = "https://sp.company.com/sites/abhi_test/_vti_bin/listdata.svc/SharedDocuments/RootFolder/Files/add(url=#TargetFileName,overwrite='true')&#TargetFileName=" + fileName;
String crlf = "\r\n";
String twoHypens = "--";
String boundary = "*****";
URL url = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setRequestMethod("POST");
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Cache-Control", "no-cache");
con.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
DataOutputStream request = new DataOutputStream(con.getOutputStream());
request.writeBytes(twoHypens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data;name=\"" + fileNameShort + "\";fileName=\"" + fileName + "\"" + crlf);
request.writeBytes(crlf);
request.write(convertToByteArray(filePath));
request.writeBytes(crlf);
request.writeBytes(twoHypens + boundary + twoHypens + crlf);
request.flush();
request.close();
InputStream responseStream = new BufferedInputStream(con.getInputStream());
BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
String line = "";
StringBuilder strBuilder = new StringBuilder();
while((line = responseStreamReader.readLine()) != null) {
strBuilder.append(line).append("\n");
}
responseStreamReader.close();
String response = strBuilder.toString();
responseStream.close();
con.disconnect();
System.out.println(response);
}
private static byte[] convertToByteArray(String filePath) {
File f = new File(filePath);
byte[] retVal = new byte[(int)f.length()];
try {
FileInputStream fis = new FileInputStream(f);
fis.read(retVal);
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch(IOException ex2) {
ex2.printStackTrace();
}
return retVal;
}
}
But I get the error
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: https://sp.web.gs.com/sites/abhi_test/_vti_bin/listdata.svc/SharedDocuments/
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1626)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at RESTFileUpload.main(RESTFileUpload.java:62)
HttpURLConnection has .getInputStream() and .getOutputStream() methods. If you wish to send body content with an Http request, you call .setDoOutput(true) on your HttpURLConnection object, call .getOutputStream() to get an Output stream and then write the content of your entity to the output stream (either as raw bytes, or using a Writer implementation of some sort), closing it when you are finished writing.
For more details, see the API docs for HttpURLConnection here.
To post files using the HttpURLConnection you have to compose the file wrapper manually. Take a look at this answer, it should be helpful for you.
I have a server and a client,
on the server side i have this handler
#Override
public void handleHttpRequest(HttpRequest httpRequest,
HttpResponse httpResponse,
HttpControl httpControl) throws Exception {
// ..
}
The question is how to send data from the client side and what method in the server side will contain the data sent?
If there is a better way to perform the communication using webbit, it will be welcomed too.
In a POST request, the parameters are sent as a body of the request, after the headers.
To do a POST with HttpURLConnection, you need to write the parameters to the connection after you have opened the connection.
This code should get you started:
String urlParameters = "param1=a¶m2=b¶m3=c";
String request = "http://example.com/index.php";
URL url = new URL(request);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches (false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
connection.disconnect();
Alternatively you could use this helper to send POST the request and get the request
public static String getStringContent(String uri, String postData,
HashMap<String, String> headers) throws Exception {
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost();
request.setURI(new URI(uri));
request.setEntity(new StringEntity(postData));
for(Entry<String, String> s : headers.entrySet())
{
request.setHeader(s.getKey(), s.getValue());
}
HttpResponse response = client.execute(request);
InputStream ips = response.getEntity().getContent();
BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));
if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK)
{
throw new Exception(response.getStatusLine().getReasonPhrase());
}
StringBuilder sb = new StringBuilder();
String s;
while(true )
{
s = buf.readLine();
if(s==null || s.length()==0)
break;
sb.append(s);
}
buf.close();
ips.close();
return sb.toString();
}
Usually one will extend HttpServlet and override doGet.
http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServlet.html
I am not familiar with webbit but I do not think it is a Servlet webserver. It mentions that it serves static pages.