post to TN from my java prg

Hi narsingv!

Very nice to get a quick reponse from you.Thanks for your suggestion!
Now what i want to do is:
I want to post a file to TN console from my java prg.For that iam using Myeclipse as development env.How could i connect to TN from my java prg.However i have written some code like this:
[highlight=java]
import java.io.;
import java.text.
;
import java.util.;
import java.net.
;
//import javax.net.ssl.HttpsURLConnection;
//import javax.net.ssl.HostnameVerifier;
//import javax.net.ssl.SSLSession;
public class FilePost{
public static void main(String args){
try{

        String userName ="Administrator";
        String password = "manage";
        String  server = "[URL="http://10.3.1.151:5555/ns/wm/tn/receive/PIP3A4PO.xml"]http://10.3.1.151:5555/ns/wm/tn/receive/PIP3A4PO.xml[/URL]";
        URL u = new URL(server);
        HttpURLConnection c = (HttpURLConnection)u.openConnection();
       // URLConnection c=u.openConnection();
                  
        c.setAllowUserInteraction(false);
        c.setRequestProperty("content-type", "application/ffdata");
        c.setRequestProperty("Authorization", (userName+":"+password));
        //c.setRequestMethod("POST");
        c.setDoOutput(true);
     //FileInputStream f = new FileInputStream("D:/Nagendra/Purchaseorder/PIP3A4PO.xml");
        //BufferedInputStream b = new BufferedInputStream(f);
        FileReader f=new FileReader("D:/Nagendra/Purchaseorder/PIP3A4PO.xml");
        BufferedReader b=new BufferedReader(f);
        OutputStreamWriter os =new OutputStreamWriter(c.getOutputStream());
        String ch;
        System.out.println("before file");
        while ((ch = b.readLine())!=null){
            os.write(ch);
            System.out.println(ch);
        }
        os.close();
        BufferedReader rd = new BufferedReader(new InputStreamReader(c.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
        }
        rd.close();
        System.out.println("after file write");
        f.close();
        os.close();
        System.out.println("after file close");
        int responseCode = c.getResponseCode();
        System.out.println(responseCode);
 
        if(responseCode != HttpURLConnection.HTTP_OK){
            System.out.println("Error sending file, error code: "+responseCode);
            System.exit(-1);
        } else {
         System.out.println("inside else");

java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(“MM/dd/yyyy hh:mm:ss SSS”);
System.out.println("Sent to TN at: "+sdf.format(new Date()));
}
System.out.println(“after try”);
} catch(Exception e){
System.out.println(“Error sending file”);
e.printStackTrace();
System.exit(-1);
}
}
}
[/highlight]
but i am getting the following error:
please help me in this regard

Error sending file
[SIZE=2][COLOR=#000080]java.io.IOException[/color][/size]: Server returned HTTP response code: 401 for URL: [url]http://10.3.1.151:5555/ns/wm/tn/receive/PIP3A4PO.xml[/url]
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at FilePost.main(
[SIZE=2][COLOR=#000080]FilePost.java:39[/color][/size])

[SIZE=2]This is my java prg which is supposed to connect to my TN and posts a doc there.This is how it looks:

import java.io.;
import java.text.
;
import java.util.;
import java.net.
;
//import javax.net.ssl.HttpsURLConnection;
//import javax.net.ssl.HostnameVerifier;
//import javax.net.ssl.SSLSession;
public class FilePost{
public static void main(String args){
try{

        String userName ="Administrator";
        String password = "manage";
        String  server = "[URL="http://10.3.1.151:5555/ns/wm/tn/receive/PIP3A4PO.xml"]http://10.3.1.151:5555/ns/wm/tn/receive/PIP3A4PO.xml[/URL]";
        URL u = new URL(server);
        HttpURLConnection c = (HttpURLConnection)u.openConnection();
       // URLConnection c=u.openConnection();
                  
        c.setAllowUserInteraction(false);
        c.setRequestProperty("content-type", "application/ffdata");
        c.setRequestProperty("Authorization", (userName+":"+password));
        //c.setRequestMethod("POST");
        c.setDoOutput(true);
     //FileInputStream f = new FileInputStream("D:/Nagendra/Purchaseorder/PIP3A4PO.xml");
        //BufferedInputStream b = new BufferedInputStream(f);
        FileReader f=new FileReader("D:/Nagendra/Purchaseorder/PIP3A4PO.xml");
        BufferedReader b=new BufferedReader(f);
        OutputStreamWriter os =new OutputStreamWriter(c.getOutputStream());
        String ch;
        System.out.println("before file");
        while ((ch = b.readLine())!=null){
            os.write(ch);
            System.out.println(ch);
        }
        os.close();
        BufferedReader rd = new BufferedReader(new InputStreamReader(c.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
        }
        rd.close();
        System.out.println("after file write");
        f.close();
        os.close();
        System.out.println("after file close");
        int responseCode = c.getResponseCode();
        System.out.println(responseCode);
 
        if(responseCode != HttpURLConnection.HTTP_OK){
            System.out.println("Error sending file, error code: "+responseCode);
            System.exit(-1);
        } else {
         System.out.println("inside else");

java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(“MM/dd/yyyy hh:mm:ss SSS”);
System.out.println("Sent to TN at: "+sdf.format(new Date()));
}
System.out.println(“after try”);
} catch(Exception e){
System.out.println(“Error sending file”);
e.printStackTrace();
System.exit(-1);
}
}
}

but i got the following error:
please help me anyone!
Error sending file
[/size][SIZE=2][COLOR=#000080]java.io.IOException[/color][/size]: Server returned HTTP response code: 401 for URL: [url]http://10.3.1.151:5555/ns/wm/tn/receive/PIP3A4PO.xml[/url]
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at FilePost.main(
[SIZE=2][COLOR=#000080]FilePost.java:39[/color][/size])

Indrani,

Why is your server pointing to “http://10.3.1.151:5555/ns/wm/tn/receive/PIP3A4PO.xml”? If you are trying to post the the Purchase Order xml to tn:receive, you should just invoke that service using
[color=blue]http://10.3.1.151:5555/invoke/wm.tn/receive[/color].

See if it helps. Rohit

Couple of suggestions:

  • On line 14, change the URL from
http://10.3.1.151:5555/ns/wm/tn/receive/PIP3A4PO.xml

to

http://10.3.1.151:5555/invoke/wm.tn/receive/PIP3A4PO.xml
  • On line 20, change the content type from application/ffdata to text/xml

  • On line 21, simply passing the username and password in this fashion is insufficient. You need to do something like this:

    c.setRequestProperty(“Authorization”, “Basic " + Base64Encoder().encode(new String(userName+”:"+password).getBytes()));

Other observations:

  • I know this is probably just a development environment so this isn’t a big deal right now but using the default Administrator account in production is not a good idea.

  • You should seriously consider not invoking wm.tn:receive directly. Rather, it can be helpful to have external systems/partners invoke a wrapper service instead. See [url]wmusers.com for additional information.

HTH

I moved the duplicate post (and replies) from thread [url]wmusers.com to this thread. Please use care in the future to post questions into the proper forum and to start new threads when appropriate.

May be I am missing something here… Rob… do we need the PIP3A4PO.xml after …/invoke/wm.tn/receive? I thought that the server url should qualify only upto the service that one is trying to invoke, which is why I suggested the change in the url. I believe PIP3A4PO.xml is the file that needs to be posted to the tn:receive.

Let me know if I am missing something here. Thanks,

Rohit

Oops. That was my mistake. The URL should be [url]http://10.3.1.151:5555/invoke/wm.tn/receive[/url] only. The doc name after not only isn’t needed but it is not allowed.