Re: POSTing binary data and nsIStringInputStream

[email protected] Thu, 22 May 2008 02:18:45 -0700 (PDT)
Newsgroups gmane.comp.mozilla.devel.java
Organization http://groups.google.com
Message-ID <14916c24-e765-4f07-897e-4530d63454c5__4035.7704427608$1211448087$gmane$org@d1g2000hsg.googlegroups.com>
Try using nsIStreamListener.

you can write a class that implements nsIStreamListener. but that
Actually Listens to a socket and every hit  to the socket will fire a
method onDataAvailable in this class.

servSock = (nsIServerSocket)
serviceManager.getServiceByContractID("@mozilla.org/network/server-
socket;1",nsIServerSocket.NS_ISERVERSOCKET_IID);
		transService = (nsISocketTransportService)
serviceManager.getServiceByContractID("@mozilla.org/network/socket-
transport-service;1",
nsISocketTransportService.NS_ISOCKETTRANSPORTSERVICE_IID);
		sockTrans = transService.createTransport(null, 0,"localhost", 7055,
null);
		servSock.init(7055, false, -1);
		listener = new SockListener(servSock, sockTrans,
serviceManager,window);
		servSock.asyncListen(listener);

this is how you may open a socket connection.
The SockListener  class u see above is a class that implements
nsIServerSocketListener.


public void onSocketAccepted(nsIServerSocket servSock,
nsISocketTransport sockTrans)
	{
		try
		{
		 System.out.println("1");
		 nsIInputStream stream =  sockTrans.openInputStream(0,0,0);
		 System.out.println("2");
		 nsIScriptableInputStream inStream =
(nsIScriptableInputStream)serviceManager.getServiceByContractID("@mozilla.org/
scriptableinputstream;1",
					nsIScriptableInputStream.NS_ISCRIPTABLEINPUTSTREAM_IID);
		 System.out.println("3");
		 inStream.init(stream);
		 System.out.println("4");

		 System.out.println("5");
		 nsIOutputStream outstream =sockTrans.openOutputStream(0,0,0);
		 String outputData = "HTTP/1.1 200 OK\n" + "Content-type: text/plain
\n\n" +"Loading account number " ;
		 outstream.write(outputData,outputData.length());

		 nsIInputStreamPump pump =
(nsIInputStreamPump)serviceManager.getServiceByContractID("@mozilla.org/
network/input-stream-pump;
1",nsIInputStreamPump.NS_IINPUTSTREAMPUMP_IID);
		 System.out.println("6");

		 pump.init(stream, -1, -1, 0, 0, false);
		 DataListener dataListener = new
DataListener(inStream,thinClientServer_popup,pump);
		 System.out.println("7");
		 pump.asyncRead(dataListener,null);
		 System.out.println("8");

		 outstream.close();
		 inStream.close();

		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
you may implement onSocketAccepted method in the above manner....

the DataListener instantiated in this method is the implementation of
the nsIStreamListner.

I think this helps you. keep me posted if you find this worth and need
any more info in the same.



On May 2, 5:07 am, [email protected] wrote:
> Hi all,
>
> I have embedded browser into my applicaction using JavaXPCOM.
> Now I need to send binary data via POST http requests (uploading files
> onto a server) .
> I found how to do this using nsIStringInputStream, it works great when
> data is text,
> but unfortunately it's method
> setData()
> gets as a parameter java String, not byte array, so it isn't usable
> for binary data.
>
> Does anybody know any sollution? Maybe there is posibility to build
>  nsI(Binary)InputStream from nsIBinaryOutputStream?
>
> here is the code I use to load page with POST attached:
>
> org.mozilla.interfaces.nsIWebNavigation wn =
> (org.mozilla.interfaces.nsIWebNavigation)
> webBrowser.queryInterface(nsIWebNavigation.NS_IWEBNAVIGATION_IID_STR);
>
> nsIComponentManager cm = Mozilla.getInstance().getComponentManager();
>
> nsIStringInputStream stringStream = (nsIStringInputStream)
> cm.createInstanceByContractID("@mozilla.org/io/string-input-stream;1"
>                 , (nsISupports) null,
> nsIStringInputStream.NS_ISTRINGINPUTSTREAM_IID);
> stringStream.setData(new String (POST), POST.length);
>
> nsIMIMEInputStream postData = (nsIMIMEInputStream)
> cm.createInstanceByContractID("@mozilla.org/network/mime-input-stream;
> 1"
>                 , (nsISupports) null, nsIMIMEInputStream.NS_IMIMEINPUTSTREAM_IID);
> postData.addHeader("Content-Type", contentType);
> postData.setAddContentLength(true);
> postData.setData(stringStream);
>
> wn.loadURI(URL, nsIWebNavigation.LOAD_FLAGS_NONE, (nsIURI) null,
> postData, null);
>
> regards
> bart