?
Solved

Java connect to C#

Posted on 2008-11-07
4
Medium Priority
?
749 Views
Last Modified: 2012-05-05
On my java code, I am trying to connect to .Net C# web server and trying to get a response.
The java code (window client application) will post a file to this C# web server and get a success flag once it is completed, how do I start with the C# code as I am not a .Net programmer?
The java code:
 
public void transferFileToServer(String url,File localFile, String fileName) throws Exception{
		HttpURLConnection urlConn = null;
		try{
 
			URL  anUrl = new URL(url);
			urlConn = (HttpURLConnection)anUrl.openConnection();
			urlConn.setDoOutput(true);
			urlConn.setDoInput(true);
 
			urlConn.connect();
			System.out.println("Swing App url connect");
 
			DataOutputStream ps = new DataOutputStream(urlConn.getOutputStream());
            		ps.writeUTF(fileName);
 
			FileInputStream fis = new FileInputStream(localFile);
			BufferedInputStream bis = new BufferedInputStream(fis);
 
			int len = 2048;
			byte[] bytes = new byte[len];
			int index = 0;
			while(index >= 0){
				index = bis.read(bytes,0,len);
				if(index != -1){
					ps.write(bytes,0,index);
				}
			}
			fis.close();
 
			ps.flush();
			ps.close();
 
			BufferedInputStream dis = new BufferedInputStream(urlConn.getInputStream());
			bytes = new byte[len];
			while(dis.read(bytes) > 0){
				//System.out.println("bytes read :["+new String(bytes)+"]");
			}
		}
		catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		finally{
			if(urlConn != null){urlConn.disconnect();}
			System.out.println("disconnect");
		}
 
    }

Open in new window

0
Comment
Question by:fkchan
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
moseack earned 750 total points
ID: 22911613
Simplest solution is to create and ASHX (ASPX.nET Generic HTTP Handler):
http://www.brainbell.com/tutorials/ASP/Generic_Handlers_(ASHX_Files).html
0
 

Author Comment

by:fkchan
ID: 22911753
Do you have sample code to read the input stream, save the file to a specific location and then return a success flag?
0
 
LVL 7

Assisted Solution

by:moseack
moseack earned 750 total points
ID: 22913149
The Context.HttpRequest.Files holds handlers to all files posted to the server as a HttpPostedFile collection.
Then you can use the HttpPostedFile.SaveAs method to save.
And then you can use Context.HttpResponse.Write to send the response.


This seems like a popular forum-thread about these kind of issues:
http://forums.asp.net/t/55127.aspx?PageIndex=2
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question