簡體   English   中英

將vb.net Web請求轉換為Java HttpwebRequest

[英]Convert vb.net web request to java HttpwebRequest

我有一個用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM