Re: HTTP response help please ?

2002-01-08 Thread Rasmy Anand

Hello Eddie
In the code you have to set the content type,.So if you are sending any data
or Object as it is, you have to give the code like this
String url;//some url intialize it
String obj=t1.getText();
URL u=new URL(url);
URLConnection cn=u.openConnection();
cn.setDoOutput(true);
cn.setDoInput(true);
cn.setUseCaches(false);

cn.setRequestProperty(Content-Type,Java-internal/+obj.getClass().getName
());
ObjectOutputStream fp1=new ObjectOutputStream(cn.getOutputStream());
fp1.writeObject(obj);
fp1.close();
ObjectInputStream in=new ObjectInputStream(cn.getInputStream());
String replay=(String)in.readObject();
t2.setText(replay);
in.close();

Regards
Rasmy :-) Enjoy Java





HTTP response help please ?

2001-11-14 Thread Eddie

Hellu,

Please some HTTP response help as it drives me grazy.
 I have a tiny java program (originaly it is part of the EJB but I extrated
it to a stand-alone program for testing) that opens a Url connection and
sends something through a Stream. It then reads the response from the input
stream.
 The strange thing is that some a jsp, and asp give me an error when trying
to open the input stream when to get the respons. I am a bit confused and
don't know the exact requirements of the receiving side to not receive this
respons error.
When I make connection to an empty asp I don't have any error, but to an asp
that has some code, I do get an error. When calling my servlets I don't get
any error. it looks like the called page should return something in a proper
way otherwhise the receiving side doesn't understand it... or something like
that. Search a lot on the web. nothing :(
Please some help on this.

 Here is the code snap, used trying to solve the problem :
-
public static void main(String[] args) {
HttpURLConnection connection=null;
  DataOutputStream out=null;

 if (args.length!=1) {
 System.out.println(To less input parameters detected);
 System.exit(-1);
  }
  String url=args[0];

 try {
  System.out.println(Trying to connect to +url);
   URL urlCon = new URL(url); // open connection with remote server
   connection = (HttpURLConnection) urlCon.openConnection();
   connection.setDoOutput(true); // indicate the we want to write output.
   connection.setDoInput (true); // indicate the we expect input.
   connection.setUseCaches (false); // no cache.

  String bla=SOMETHING;

   connection.setRequestProperty(CONTENT_LENGTH,  + sms.length());
   out = new DataOutputStream(connection.getOutputStream());
   System.out.println(Trying to send: +bla);
   out.writeBytes (bla);
   out.flush ();
   out.close(); // free shared resources.

   // Getting response code/string from remote server.

 DataInputStream input;
 InputStream resStrm = connection.getInputStream ();
 // HERE IS WERE
 COMPLAINS.
 int res = connection.getResponseCode();
 System.out.println(Return: +res);

 input = new DataInputStream (resStrm);
 String str;
   while (null != ((str = input.readLine(
System.out.println (str);

 -

 The error:
 --
 java.io.FileNotFoundException: http://10.17.17.213/m2u/interfaces/g
 aap.asp at sun.net.www.protocol.http.HttpURLConnection.getInputStre
 am(Unknown Source)  at java.net.HttpURLConnection.getResponseCode(Unknown
Source)
 at connect2Url.main(connect2Url.java:49)
 ---


 The test jsp to receive it, that doesn't work:

System.out.println(Receiving something);

  BufferedReader in = new BufferedReader(new
 InputStreamReader(request.getInputStream()));
  String inputLine = ;
  String totLine = ;

  while ((inputLine = in.readLine()) != null) {
totLine = totLine + inputLine;
   }
  in.close();   // close input stream.
System.out.println(Received: +totLine);

response.setContentType(text/html);
response.setStatus(200);
 

When I call an empty asp page it all goes well ... :(

Regards,
Ed Bras