C# 到 java HTTPWebRequest 转换

标签 c# java httpwebrequest httpurlconnection

我正在尝试将一些代码从 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();

关于C# 到 java HTTPWebRequest 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20331378/

相关文章:

c# - HTTPWebResponse 不返回任何内容

c# - 在 Windows 8 上发送帖子数据

c# - 从 chrome 扩展到用 C# 编写的 native 主机的 native 消息传递

java - IntelliJ 设置堆大小超过 4GB。如何?

c# - HTTP Web 请求包装器/帮助器库

c# - 在 C# 中通过 HttpWebRequest 实现 Digest 身份验证

c# - P/从 c# 调用非托管 C++ 代码 - 获取 "tried to access protected memory error"

c# - 对Cookie进行加密编码

java - 无法从组合键检索准确的 ID

抛出新的 NPE 时不需要 Java 'throws' 子句?为什么添加 "throws Exception"会出现编译错误?