gpt4 book ai didi

C# 到 java HTTPWebRequest 转换

转载 作者:行者123 更新时间:2023-12-01 18:40:01 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com