Fire multiple requests with URLConnection socket flush - java

What did wrong here? I want to send multiple messages in one https connection with URLConnection. I only get the first message on server.
URL url = new URL("https://example.com:443");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(connection.getOutputStream()));
out.write("Hello");
out.flush();
inReader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
out.write("Response");
out.flush();
inReader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
out.write("Response2");
out.flush();
inReader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
out.write("Response3");
out.flush();
inReader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
inReader.close();
out.close();

You can't. URLConnection is for HTTP, which is a stateless protocol. Not for your own stateful messaging protocol. One request and one response. If you want to send another message, get a new URLConnection. Connection pooling will probably happen behind the scenes.
You also need to read the response. Merely getting the input stream is not sufficient, and getting it multiple times is pointless.
Hard to see why you are writing "Response" in a request.

Related

java.lang.IllegalStateException: Cannot use BufferedReader while ServletInputStream is in use

Can Java experts help me on this issue, I'm geting the error below
java.lang.IllegalStateException: Cannot use BufferedReader while ServletInputStream is in use
I'm calling a REST service url to post some data using a server side java code, not through a browser.
try{
String urlLocation = "http://myserver/james/dev/hello.nsf/services.xsp/test";
URL url = new URL(urlLocation);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setAllowUserInteraction(false);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
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 (getXML());
out.flush ();
out.close ();
connection.disconnect();
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while( (line = in.readLine()) !=null) {
System.out.println(line);
}
in.close();
}
catch(Exception e){
System.out.println("Error from 2nd try statement");
e.printStackTrace();
e.toString();
}
Any help will be greatly appreciated...
It seems to be this line
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
I noticed the BufferedReader is not being instantiated, you are reusing an instance variable in.
I copied your code and just changed it to this, and I can make multiple requests without any problem.
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));

Character encoding in web applicaion running on Websphere Application Server 8.5

I have a problem with Cyrillic symbols represention (they looks as '0B0;lO') in IBM WebSphere Application I created.
So I use WebSphere 8.5 (Unix Red Hat) and application is a java-servlet which listens to HTTP POST Requests.
JVM properties on WebSphere are:
Default Charset=UTF-8
Default Charset= in use=UTF-8
file.encoding=UTF-8
I have checked file encoding.properties and set encoding to UTF8. Requests to my application I send in UTF8 by following code:
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Content-Type", "text/plain; charset=UTF-8");
connection.setRequestProperty("Content-Length", Integer.toString(resultXML.getBytes().length));
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
// BufferedWriter br = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
BufferedWriter br1 = new BufferedWriter(new OutputStreamWriter(wr, Charset.forName("UTF-8")));
br1.write(resultXML);
br1.close();
wr.flush ();
wr.close ();
//Get Response
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
rd.close();
In application HTTP Request Handler first of all I set encoding by:
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
But all Cyrillic symbols in request and response looks uncorrectly and when appplication put them to Oracle DB or simply to response these symbols have wrong view.
Help with problem, please!
This issue is resolved.
Request Cyrillic symbols was processed correctly, problem was with response. Following code helped.
PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
for (int i = 0; i < result.length(); i++)
{
out.print(result.charAt(i));
}
out.flush();

Client waiting for reply from server though server has sent it

I have a code like the one below
Server side:
Socket socket = server-client conn socket
try
{
BufferedReader inFromNode = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
PrintWriter outToNode = new PrintWriter(socket.getOutputStream(), true);
String data = inFromNode.readLine().toString();
String data1 = inFromNode.readLine().toString();
String data2 = inFromNode.readLine().toString();
outToNode .println("Hi");
}
Client side:
Socket socket = server-client conn socket
try
{
BufferedReader inFromNode = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
PrintWriter outToNode = new PrintWriter(socket.getOutputStream(), true);
outToNode .println("Hi");
outToNode .println("Hi");
outToNode .println("Hi");
String data = inFromNode.readLine().toString();
}
The problem is the client side code is waiting for the reply from the server. I am sure the server side has sent it(I tried placing logs after the send on the server side and they got printed.) Am I overlooking on something here? Is the code wrong in any way?
Try closing the PrintWriter and the Socket when writing to client finishes from Server. This should ideally fix your problem.

tcp/ip open connection

currently i am using the following code to interact with server
public String connectToserverforincomingmsgs(String phonurl, String phno)
throws IOException {
URL url = new URL(phonurl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
// Allow Outputs
con.setDoOutput(true);
con.connect();
BufferedWriter writer = null;
writer = new BufferedWriter(new OutputStreamWriter(
con.getOutputStream(), "UTF-8"));
// give server your all parameters and values (replace param1 with you
// param1 name and value with your one's)
writer.write("sender_no=" + phno);
writer.flush();
String responseString = "";
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
responseString = responseString.concat(line);
}
con.disconnect();
return responseString;
}
how could i make tcp connection .right now i don't have any idea . i am new to android and java aswell so any sample code about the tcp connection would be appreciated
To create a TCP Connection you need to Use Socket:
Socket socket = new Socket(host_name_or_ip_address, port_no);
To Send Data use socket.getOutputStream()
To Receive Data use socket.getInputStream()
Just replace HttpURLConnection with Socket. It works pretty much the same

Problem with the encoding of a web page

I'm trying to get some information from a web... with the code above...
URL url = new URL(webpage);
URLConnection connection;
connection = url.openConnection();
BufferedReader in;
InputStreamReader inputStreamReader;
inputStreamReader = new InputStreamReader(connection.getInputStream(), "iso-8859-1");
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
But I'm having a problem with the encoding when I'm reading it. The page is in spanish, and it has some simbols like "ñ" or "á". The header of the source code of the page says that it's in "iso-8859-1", and I've tried with "utf-8", but none of them works... when I try to set the text I'm reading from the URL to a TextView it just shows garbage in the simbols I've told....
Any ideas?
Thanks!
I think you are creating the reader incorrectly
inputStreamReader = new InputStreamReader(connection.getInputStream(), "iso-8859-1");
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
The first statement is creating a Reader with the specified encoding, but the second one is ignoring the original Reader and creating a new one with the default encoding for your platform. You probably need to do this:
inputStreamReader = new InputStreamReader(connection.getInputStream(), "iso-8859-1");
in = new BufferedReader(inputStreamReader);

Categories

Resources