locked
Convert Java code to c# or vb RRS feed

  • Question

  • Could some help convert this to vb or C#?

    import java.util.concurrent.TimeUnit;
    import java.io.*;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    import org.apache.http.impl.client.BasicResponseHandler;
    import org.json.JSONObject;
    import org.json.JSONArray;


    public class Sample
    {

        public static void main(String[] args) {

            String apiKey = "xxxxxxxxxxxxxxxx";
            String userIdentityId = "12345"; you want to run
            String testExportName = "Export";
            String outputFileName = "file.csv"; 
            String exportListURL = "https://api.web";
            int pollingInterval = 30;
            HttpClient httpClient = HttpClientBuilder.create().build();

            HttpGet httpGet = new HttpGet(exportListURL);
            httpGet.setHeader("x-api-key",apiKey);

            String testExportId = "";

            // Step 1:  Loop through the JSON of all of my available exports and find the export ID of my export, given the export name
            try {

                ResponseHandler<String> responseHandler=new BasicResponseHandler();
                String responseBody = httpClient.execute(httpGet, responseHandler);
                JSONObject response=new JSONObject(responseBody);

                JSONArray exportList = response.getJSONArray("exports");

                for(int i = 0; i < exportList.length(); i++)
                {
                      JSONObject export = exportList.getJSONObject(i);

                      if(export.get("name").equals(testExportName)) // We found the correct export, so record the ID
                      {
                          testExportId = export.get("id").toString();
                          break;
                      }
                }

    }

    }

    }


    • Edited by bagua8 Wednesday, October 14, 2015 2:16 PM
    Wednesday, October 14, 2015 2:15 PM

Answers

  • Hi bagua8,

    There is a free/commercial tool to convert Java to C# code available you could use that. Microsoft also has tool to convert Java code into C#. Hope this helps you.

    Java to C# converter

    Microsoft's Tool: Java Language Conversion Assistant 2.0

    Thanks,

    Sabah Shariq



    • Edited by Sabah ShariqMVP Wednesday, October 14, 2015 2:34 PM
    • Proposed as answer by ryguy72 Thursday, October 15, 2015 2:22 AM
    • Marked as answer by Kristin Xie Monday, October 26, 2015 1:54 AM
    Wednesday, October 14, 2015 2:30 PM
  • i'm more concerned what would replace these

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    import org.apache.http.impl.client.BasicResponseHandler;
    import org.json.JSONObject;
    import org.json.JSONArray;

    @bagua8

    I am not a expert on Java, but I try to find as below in C#.

    HttpRequest ClassHttpResponse Class

    Both of above should add in System.Web.dll.About Jason you could use Json.NET.

    Actually, I don't recommend you use some convert tool to help you. And it always build with some errors. Please  try to learn some C# basics and try to implement your yourself. You'll get more than before.

    Best regards,

    Kristin

     


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Kristin Xie Monday, October 26, 2015 1:54 AM
    Thursday, October 15, 2015 9:21 AM

All replies

  • Hi bagua8,

    There is a free/commercial tool to convert Java to C# code available you could use that. Microsoft also has tool to convert Java code into C#. Hope this helps you.

    Java to C# converter

    Microsoft's Tool: Java Language Conversion Assistant 2.0

    Thanks,

    Sabah Shariq



    • Edited by Sabah ShariqMVP Wednesday, October 14, 2015 2:34 PM
    • Proposed as answer by ryguy72 Thursday, October 15, 2015 2:22 AM
    • Marked as answer by Kristin Xie Monday, October 26, 2015 1:54 AM
    Wednesday, October 14, 2015 2:30 PM
  • i'm more concerned what would replace these

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    import org.apache.http.impl.client.BasicResponseHandler;
    import org.json.JSONObject;
    import org.json.JSONArray;

    Wednesday, October 14, 2015 3:25 PM
  • i'm more concerned what would replace these

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    import org.apache.http.impl.client.BasicResponseHandler;
    import org.json.JSONObject;
    import org.json.JSONArray;

    @bagua8

    I am not a expert on Java, but I try to find as below in C#.

    HttpRequest ClassHttpResponse Class

    Both of above should add in System.Web.dll.About Jason you could use Json.NET.

    Actually, I don't recommend you use some convert tool to help you. And it always build with some errors. Please  try to learn some C# basics and try to implement your yourself. You'll get more than before.

    Best regards,

    Kristin

     


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Kristin Xie Monday, October 26, 2015 1:54 AM
    Thursday, October 15, 2015 9:21 AM
  • using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Web;

    /// <summary>
    /// Summary description for APIHandler
    /// </summary>
    public class APIHandler
    {

        string Method = "";
        string ContentType = "";
        public APIHandler(bool isPost = true, string ContentType = "application/json")
        {
            if (isPost)
            {
                Method = "POST";
            }
            else
            {
                Method = "GET";
            }
            this.ContentType = ContentType;

        }
        public string ApiRequest(string DATA)
        {
            try
            {
                string Url = ConfigurationManager.AppSettings["URL"].ToString();
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = Method;
                request.ContentType = ContentType;
                StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
                requestWriter.Write(DATA);
                requestWriter.Close();
                try
                {
                    WebResponse webResponse = request.GetResponse();
                    Stream webStream = webResponse.GetResponseStream();
                    StreamReader responseReader = new StreamReader(webStream);
                    string response = responseReader.ReadToEnd();
                    //Console.Out.WriteLine(response);
                    responseReader.Close();
                    return response;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message.ToString());
                }
            }
            catch (Exception ex)
            {
                LogWriter.WriteLog("Error in method ApiRequest :" + ex.Message.ToString());
                throw new Exception(ex.Message.ToString());

            }
        }

    }

    i created this code for call rest service  json type response.  this response is json read this json string according to your response format.

    Tuesday, September 12, 2017 12:53 PM