如何将此php代码转换为Java?

丹尼

任何人都可以帮助将以下PHP代码转换为Java代码吗?我更喜欢使用Apache cxf webclient发送请求,但是欢迎任何解决方案。

$services_url = 'http://example/services_wgea_drupal';


//(1) Server REST - user.login
// REST Server URL for auth
$request_url = $services_url . '/user/login';
// User data
    $user_data = array(
  'username' => 'aaaa',
  'password' => 'bbbb',
);
$user_data = http_build_query($user_data);
// cURL
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json')); // Accept JSON response
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, 1); // Do a regular HTTP POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $user_data); // Set POST data
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($curl);

这是我尝试的:

WebClient client =
        WebClient.create("http://example/services_wgea_drupal").accept(MediaType.APPLICATION_JSON);
    client.path("/user/login").type(MediaType.APPLICATION_JSON);
    Response response = client.post("{\"username\":\"aaaa\",\"password\":\"bbbb\"}");

响应代码为200,但根据我的PHP同事的说法,应返回包含会话名称和会话ID的json消息。但是响应的responseMessage属性如下所示。我认为这不是正确的回答。

{org.apache.cxf.rest.message=true, 
org.apache.cxf.invocation.context={ResponseContext={}, 

RequestContext={response.class=class javax.ws.rs.core.Response, 
org.apache.cxf.request.method=POST, 
org.apache.cxf.request.uri=http://example/services_wgea_drupal/user/login, 
response.type=class javax.ws.rs.core.Response, 
request.class=class java.lang.String, 
jaxrs.proxy=false, org.apache.cxf.message.Message.PROTOCOL_HEADERS={Accept=[application/json], 
Content-Type=[application/json]}, request.type=null, 
org.apache.cxf.message.Message.ENDPOINT_ADDRESS=http://example/services_wgea_drupal/user/login}},
http.scheme=http, org.apache.cxf.client=true, org.apache.cxf.message.inbound=false, 
org.apache.cxf.message.Message.PROTOCOL_HEADERS={Accept=[application/json], Content-Type=            [application/json]}, 
org.apache.cxf.message.Message.ENDPOINT_ADDRESS=http://example/services_wgea_drupal/user/login, 
org.apache.cxf.request.method=POST,          org.apache.cxf.request.uri=http://example/services_wgea_drupal/user/login, 
jaxrs.proxy=false,    http.connection=sun.net.www.protocol.http.HttpURLConnection:http://example/services_wgea_drupal/user/login, 
 Content-Type=application/json, jaxrs.template.parameters=null, 
 org.apache.cxf.transport.Conduit=conduit: class     org.apache.cxf.transport.http.URLConnectionHTTPConduit737704879target:    http://example.com/services_wgea_drupal/user/login,          org.apache.cxf.message.Message.BASE_PATH=http://example.com/services_wgea_drupal}
鲁斯兰尼
public static String sendPost(String url, String params) throws Exception {
    URL obj = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) obj.openConnection();

    // Act like a browser
    connection.setUseCaches(false);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Accept", "application/json");
    connection.setRequestProperty("Connection", "keep-alive");
    connection.setRequestProperty("Content-Length", Integer.toString(params.length()));

    connection.setDoOutput(true);
    connection.setDoInput(true);

    // Send post request
    DataOutputStream out = new DataOutputStream(connection.getOutputStream());
    out.writeBytes(params);
    out.flush();
    out.close();

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "Windows-1251"));
    StringBuilder response = new StringBuilder();
    String inputLine;

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }

    in.close();

    return response.toString();
}

这种方法应该可以帮助您。您可以像这样使用它:

sendPost("http://example/services_wgea_drupal/user/login", "username=aaaa&password=bbbb");

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章