0

I am trying to do convert some code from C# to Java. It is to get data which is returned in XML format. I first used a conversion tool then tried the rest out by hand but now I am stuck. Please assist

using System;
using System.Collections.Generic; 
using System.Linq;
using System.Text; 
using System.Net; 
using System.IO; 
using System.Xml; 
using System.Xml.Linq;
using System.Web;


namespace ConsoleApplication1 
 { 
class Program
  { 
static void Main(string[] args)
    {

        //System.Net.WebRequest.GetSystemWebProxy();
        string urlDemo = "http://www.secret.com/api"; 

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlDemo);

    // Set the Method property of the request to POST. 
    request.Method = "POST"; 
    // Create POST data and convert it to a byte array. 
    string postData = "api_username=username&api_password=password";
    postData += "&MODULE=WithDrawals&COMMAND=view";
    byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
    // Set the ContentType property of the WebRequest. 
    request.ContentType = "application/x-www-form-urlencoded"; 
    // Set the ContentLength property of the WebRequest. 
    request.ContentLength = byteArray.Length; request.Timeout = 60000; 
    // Get the request stream.
    Stream dataStream = request.GetRequestStream(); 
    // Write the data to the request stream. 
    dataStream.Write(byteArray, 0, byteArray.Length); 
    // Close the Stream object.

    // Get the response. 
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    // Display the status. 
    Console.WriteLine(((HttpWebResponse)response).StatusDescription); 
    // Get the stream containing content returned by the server. 
    dataStream = response.GetResponseStream(); 
    // Open the stream using a StreamReader for easy access. 
    StreamReader reader = new StreamReader(dataStream);
    // Read the content. 
    string responseFromServer = reader.ReadToEnd(); 
    // Display the content. 
    Console.WriteLine(responseFromServer); 
    Console.WriteLine("\nClick On Enter To Close Window");
    Console.ReadLine();
    // Clean up the streams. 
    reader.Close(); 
    dataStream.Close(); 
    response.Close(); 
} 

} }

In Java I have so far but the second line is not working

   package ConsoleApplication;

import java.util.*;
import java.io.*;
import java.net.*;

public class ConsoleApplication
 {
  static void main(String[] args)
    {

    //System.Net.WebRequest.GetSystemWebProxy();
    String urlDemo = "http://www.secret.com/api";

     HttpURLConnection  request = (HttpURLConnection)WebRequest.Create(urlDemo);


// Set the Method property of the request to POST. 
request.setRequestMethod("POST");
// Create POST data and convert it to a byte array. 
String postData = "api_username=username&api_password=password";
postData += "&MODULE=WithDrawals&COMMAND=view";

byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest. 
request.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
// Set the ContentLength property of the WebRequest. 
request.setRequestProperty("Content-Length", "" + 
           Integer.toString(urlDemo.getBytes().length));
request.setReadTimeout(6000);
// Get the request stream.
Stream dataStream = request.getInputStream();
// Write the data to the request stream. 
dataStream.Write(byteArray, 0, byteArray.length);
// Close the Stream object.

// Get the response. 
HttpResponse response = (HttpResponse)request.GetResponse();
// Display the status. 
System.out.println(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server. 
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access. 
Reader reader =  new InputStreamReader(dataStream);
// Read the content. 
int responseFromServer = reader.read();
// Display the content. 
System.out.println(responseFromServer);
System.out.println("\nClick On Enter To Close Window");
new Scanner(System.in).nextLine();
// Clean up the streams. 
reader.close();
dataStream.Close();
response.Close();
}

}

user2533398
  • 161
  • 1
  • 2
  • 12
  • 4
    *"the second line is not working"* ..What on Earth made you think the last part of the 2nd line would work? It does not even follow the Java naming conventions and is just ..copy/pasted from the C#! – Andrew Thompson Dec 02 '13 at 15:06
  • http://stackoverflow.com/questions/15856215/httpurlconnection-app-engine-java-example-for-post-request-does-not-work – kosa Dec 02 '13 at 15:06

1 Answers1

4

This is how you do it in Java:

URL obj = new URL(urlDemo);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
sanket
  • 789
  • 4
  • 16