Sending a HTTP POST Request - java

I am trying to send a request to get public transport information. Here's a screenshot of an example below, stating that I must send an XML request to the site, defining the method and the service reference (in the example it's StopMonitoringRequest and 020035811).
So far I have managed to connect to the service, but I have no idea what to do from here. I have so far done this...
String user = "";
String pass = "";
String url = "http://nextbus.mxdata.co.uk/nextbuses/1.0/1";
String authString = user + ":" + pass;
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.setRequestMethod("POST");
connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty( "charset", "utf-8");
connection.setUseCaches(false);
connection.setDoOutput(true);
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int numCharsRead;
char[] charArray = new char[1024];
StringBuffer sb = new StringBuffer();
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
String result = sb.toString();
System.out.print(result);
...receiving this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Siri version="1.0" xmlns="http://www.siri.org.uk/">
<ServiceDelivery>
<ResponseTimestamp>2015-11-08T20:33:03.574Z</ResponseTimestamp>
</ServiceDelivery>
</Siri>
How do I enter the required parameters and method?

So what I had to do was create a HttpPost and set the xml request up as an entity, binding it to the post. Here is the code, in case anyone wants to request information via HTTP POST using XML, outputting the XML as a string:
// basic autthorization security
String url = "http://nextbus.mxdata.co.uk/nextbuses/1.0/1";
String authString = "<username>:<password>";
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
post.setHeader("Authorization", "Basic " + authStringEnc);
StringEntity input = new StringEntity(request);
input.setContentType("text/xml");
post.setEntity(input);
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
String unformattedXML = EntityUtils.toString(entity);

Related

IOException in Basic Authentication for HTTP url

I am hitting HTTP url with username:password using JAVA code. Below is my code
public static main (String args[]){
try{
String webPage = "http://00.00.000.000:8080/rsgateway/data/v3/user/start/";
String name = "abc001";
String password = "abc100";
String authString = name + ":" + password;
System.out.println("auth string: " + authString);
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
URL url = new URL(webPage);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setUseCaches(true);
connection.setRequestMethod("GET");
connection.setRequestProperty("Authorization","Basic " +authStringEnc);
connection.setRequestProperty("Accept", "application/xml");
connection.setRequestProperty("Content-Type", "application/xml");
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int numCharsRead;
char[] charArray = new char[1024];
StringBuffer sb = new StringBuffer();
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
String result = sb.toString();
System.out.println("*** BEGIN ***");
System.out.println(result);
System.out.println("*** END ***");
} catch (Exception e) {
e.printStackTrace();
}
}
But I am getting 401 Error
java.io.IOException: Server returned HTTP response code: 401 for URL:
same url if i hit using curl then it is returning response. below is the curl command.
curl -u abc001:abc100 http://00.00.000.000:8080/rsgateway/data/v3/user/start/
Please help me resolve this.
The code you are getting is HTTP 401 Unauthorized, which means that the server isn't interpreting your basic authentication properly.
Since you say that the curl command with the basic authentication you showed is working, I'll assume the problem is in your code.
It looks like you tried to follow this code.
The only error I can see (but I cannot test this to be sure) is that you just cast the byte[] to a String instead of encoding it with Base64.
So you should change this:
String authStringEnc = new String(authEncBytes);
to this:
String authStringEnc = Base64.getEncoder().encodeToString(authEncBytes);
Also, you want to change this:
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
to this:
byte[] authEncBytes = authString.getBytes();
byte[] authEncBytes = authString.getBytes(StandardCharsets.UTF_8);

UTF-8 problem in response of SOAP request

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");

Problems with setting Authorization in request headers from Java

I am facing a problem where I am not able to set the "Authorization" Header.
I am Able to set the rest of the headers but when I am using the particular Key I am not able to set any data. Please help.
URL myURL = new URL(url);
HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
String basicAuth = "Bearer 6f6b06fe-131e-314b-9ef8-42f2cbdcfc18";
myURLConnection.setRequestMethod("GET");
myURLConnection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
myURLConnection.setRequestProperty("Content-Language", "en-US");
myURLConnection.setRequestProperty("Authorization", "basicAuth");
myURLConnection.setUseCaches(false);
myURLConnection.setDoInput(true);
myURLConnection.setDoOutput(true);
System.out.println(myURLConnection.getRequestProperties());
Hoping to hear soon. Thank you.
The statement
myURLConnection.getRequestProperties()
does not list all headers.
By looking at the source of HttpURLConnection you notice Authorization is part of the headers excluded by HttpURLConnection#getRequestProperties.
http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/484e16c0a040/src/share/classes/sun/net/www/protocol/http/HttpURLConnection.java
This does not mean the header isn't set.
I think You made a little mistake there, The value for the key Authorization must not be "basicAuth". So please replace the code with :
myURLConnection.setRequestProperty("Authorization", basicAuth);
Or try this :
String basicAuth = "Bearer 6f6b06fe-131e-314b-9ef8-42f2cbdcfc18";
String encodedAuth= Base64.encode(basicAuth.getBytes());
myURLConnection.setRequestProperty("Authorization", encodedAuth);
Try with following code:
public void sendPost(String URL, String jsonData, String authUrl) throws Exception {
post = new HttpPost(URL);
// add header
post.setHeader("Authorization", accessToken);
post.setHeader("User-Agent", USER_AGENT);
if (!jsonData.isEmpty()) {
post.setEntity(new StringEntity(jsonData, ContentType.create("application/json")));
}
client = HttpClientBuilder.create().build();
response = client.execute(post);
outputFile = new File("path of file");
fos = new FileOutputStream(outputFile);
headers = response.getAllHeaders();
bw = new BufferedWriter(new OutputStreamWriter(fos));
for (Header header : headers) {
bw.write(header.getName() + ": " + header.getValue() + "\n");
}
bw.write("Response Code : " + response.getStatusLine());
bw.close();
}

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?

get JSON response while performing POST request with HttpURLConnection

I am using the following code to perform POST requests on a REST API. It is all working fine. What I am being unable to do is after POST is successful the API returns response JSON in body with headers, this JSON has information which I require. I am unable to get the JSON response.
I need this response as this response includes the ID generated by DB. I can see the response while using REST Client plugin of firefox. Need to do implement the same in Java.
String json = "{\"name\": \"Test by JSON 1\",\"description\": \"Test by JSON 1\",\"fields\": {\"field\": []},\"typeDefinitionId\": \"23\",\"primaryParentId\": \"26982\"}";
String url = "http://serv23/api/contents";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//Setting the Request Method header as POST
con.setRequestMethod("POST");
//Prepairing credentials
String cred= "user123:p#ssw0rd";
byte[] encoded = Base64.encodeBase64(cred.getBytes());
String credentials = new String(encoded);
//Setting the Authorization Header as 'Basic' with the given credentials
con.setRequestProperty ("Authorization", "Basic " + credentials);
//Setting the Content Type Header as application/json
con.setRequestProperty("Content-Type", "application/json");
//Overriding the HTTP method as as mentioned in documentation
con.setRequestProperty("X-HTTP-Method-Override", "POST");
con.setDoOutput(true);
JSONObject jsonObject = (JSONObject)new JSONParser().parse(json);
OutputStream os = con.getOutputStream();
os.write(jsonObject.toJSONString().getBytes());
os.flush();
WriteLine( con.getResponseMessage() );
int responseCode = con.getResponseCode();
Get the input stream and read it.
String json_response = "";
InputStreamReader in = new InputStreamReader(con.getInputStream());
BufferedReader br = new BufferedReader(in);
String text = "";
while ((text = br.readLine()) != null) {
json_response += text;
}

Categories

Resources