Java connect to C#

Posted on 2008-11-07
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;


			URL  anUrl = new URL(url);

			urlConn = (HttpURLConnection)anUrl.openConnection();




			System.out.println("Swing App url connect");

			DataOutputStream ps = new DataOutputStream(urlConn.getOutputStream());


			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 =,0,len);

				if(index != -1){







			BufferedInputStream dis = new BufferedInputStream(urlConn.getInputStream());

			bytes = new byte[len];

			while( > 0){

				//System.out.println("bytes read :["+new String(bytes)+"]");



		catch(Exception e){


			throw e;



			if(urlConn != null){urlConn.disconnect();}




Open in new window

Question by:fkchan
    LVL 7

    Accepted Solution

    Simplest solution is to create and ASHX (ASPX.nET Generic HTTP Handler):

    Author Comment

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

    Assisted Solution

    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:

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now