繁体   English   中英

C#到Java HTTPWebRequest的转换

[英]C# to java HTTPWebRequest conversion

我正在尝试将一些代码从C#转换为Java。 它是为了获取以XML格式返回的数据。 我首先使用了转换工具,然后手动尝试了其余部分,但现在我陷入了困境。 请协助

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(); 
} 

}}

在Java中,到目前为止,第二行不起作用

   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();
}

}

这是您在Java中的操作方式:

URL obj = new URL(urlDemo);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM