gpt4 book ai didi

java - 将 vb.net web 请求转换为 java HttpwebRequest

转载 作者:太空宇宙 更新时间:2023-11-04 15:05:02 26 4
gpt4 key购买 nike

我有一个用 vb.net 编写的工作 Web 请求,我想将其转换为 java Web 请求,查询的其余服务返回并需要 json 中的参数,我使用 vb.net 中的 Newtonsoft.Json 来构造参数。我如何在java中做到这一点?

 Output is the  json as in:
Dim sw As New StringWriter
Dim jsonWriter As JsonWriter = New JsonTextWriter(sw)
Dim Output As String
jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented
Output = "vehiclecheck?"
jsonWriter.WriteStartObject()
jsonWriter.WritePropertyName("fault")
jsonWriter.WriteValue("1")
jsonWriter.WriteEndObject()
Output = Output & "where=" & sw.ToString

Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
Dim cw As Integer = 0
Try
If Output = Nothing Then Exit Function
request = DirectCast(WebRequest.Create("https://api.appery.io/rest/1/db/collections/" & myFilter), HttpWebRequest)
request.Credentials = New NetworkCredential("user_name", "password")
request.Headers.Add("X-Appery-Database-Id:50b3a600e4b0747xxxxxxxx")
' Get response
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
Dim results = JsonConvert.DeserializeObject(reader.ReadToEnd())

For R = 0 To results.count - 1
'process results
next

我尝试使用您提供的示例,但没有成功。我已将 org.apache.commons.httpclient 库添加到我的应用程序中,并构造了以下类并传递了正确的 URL,您能给我建议吗?

RequestTask 类扩展 AsyncTask{

@Override
protected String doInBackground(String... uri) {
String username = "username";
String password = "password";
String host = "com.httptest.myapp";

HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(uri[0]);
HostConfiguration hostCfg = new HostConfiguration();

HttpState state = client.getState();

method.addRequestHeader("X-Appery-Database-Id", "50b3a600e4b0747d4xxxxxx");
//setting a proxy, for example:
state.setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
hostCfg.setProxyHost(new ProxyHost(host, AuthScope.ANY_PORT));

//method.getParams().setSoTimeout(timeout); for example a timeout

try {
client.executeMethod(hostCfg, method);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //calling the server
try {
String resp = method.getResponseBodyAsString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //the response body


return null;


}
}

最佳答案

您可以使用httpclient框架,很简单;下面是一些例子:

HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(URIget);
HostConfiguration hostCfg = new HostConfiguration();

HttpState state = client.getState();

//setting a proxy, for example:
state.setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(getProxyUsername(), getProxyPassword()));
hostCfg.setProxyHost(new ProxyHost(getProxyHost(), getProxyPort()));

//method.getParams().setSoTimeout(timeout); for example a timeout

client.executeMethod(hostCfg, method); //calling the server
String resp = method.getResponseBodyAsString(); //the response body

关于java - 将 vb.net web 请求转换为 java HttpwebRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22115665/

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