0

我用java方法编写了负责发送的POST方法ajax

public String getWebInfo() {

    DefaultHttpClient httpclient = null;
    String out = null;

    try {
        System.out.println("send started");
        httpclient = new DefaultHttpClient();

        Credentials cred = new UsernamePasswordCredentials("user", "password");


         httpclient.getCredentialsProvider().setCredentials(
                    new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
                    cred);

         HttpPost httpost = new HttpPost("https://page/php/data.ajax.php");

        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("action", "1"));
        nvps.add(new BasicNameValuePair("name", "2"));

        UrlEncodedFormEntity entityU = new UrlEncodedFormEntity(nvps);
        entityU.setContentEncoding(HTTP.UTF_8);
        //entityU.setContentType("application/json");
        httpost.setEntity(entityU);



        System.out.println("send about to do post");
        HttpResponse response = httpclient.execute(httpost);
        System.out.println("send post done");
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            System.out.println("response.getStatusLine: " + response.getStatusLine());
            InputStream is = entity.getContent();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ( (line = br.readLine()) != null) {
                System.out.println("Response: " +  line);
                out = line;
            }
            is.close();

        } else {
            System.out.println("Response: response is null");
        }

    } catch (Exception e) {
        e.getStackTrace();
    }

    if (httpclient != null) {
        httpclient.getConnectionManager().shutdown();
    }

    return out;     
}

输出为空:

send started
send about to do post
send post done
response.getStatusLine: HTTP/1.1 200 OK
Response: []

服务器端

从我的登录msqLogFile("page/post",Array('post' => urldecode(implode(", ", $_POST))));

我只得到没有密钥的数据:

"2013-05-02 04:21:09","1,2"

它应该是 json 数据:

tail -f /etc/httpd/logs/ssl_request_log

 82.80.25.130 TLSv1 AES128-SHA "POST /page/php/data.ajax.php HTTP/1.1" 484
 82.80.25.130 TLSv1 AES128-SHA "POST /page/php/data.ajax.php HTTP/1.1" 3

当我取消注释时entityU.setContentType("application/json");,我根本没有得到任何数据。

从javascript一切正常:

var json = JSON.stringify({action: 1,name: 2});
        $.ajax({
            type: "POST",
            url: "/page/php/data.ajax.php",
            dataType:"json",
            data:{data:json},
            success:function(data){

在这里我得到回应:

"2013-05-02 04:21:09","{"action":1,"name":2}"

我的问题在哪里?

谢谢,

4

2 回答 2

0

很好,你找到了。我通过帖子阅读并查看了您的测试方式并提出了建议。

如果你使用了大量的 http,那么你会在开发中得到很好的结果,因为你能够 CURL 你所有的表达式,以便看到你在网络上的工作流程是好的。如果您需要,您的单元测试可以包括只发送大量 http 流量的 Bash 脚本......

在 java 中,您需要找到一种方法来切换 http 日志,以便您的普通记录器可以编写如下所示的 HEADER/WIRE 语句--请参阅接受 ans--

您将清楚地看到服务器的所有标头 TO 和 FRM,WIRE 上的所有正文信息到/frm 在您的普通记录器中以方向“<<”或“>>”显示的服务器。

弄清楚如何去做很棘手,但是当你弄清楚时值得一读 IMO。

于 2013-05-03T14:59:25.263 回答
0

我找到了解决方法,但假设有人会找到更好的响应:

public String getWebInfo() {

DefaultHttpClient httpclient = null;
String out = null;

try {
    System.out.println("send started");
    httpclient = new DefaultHttpClient();

    Credentials cred = new UsernamePasswordCredentials("user", "password");


     httpclient.getCredentialsProvider().setCredentials(
                new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
                cred);

     HttpPost httpost = new HttpPost("https://page/php/data.ajax.php");

     JSONObject jsonObj = new JSONObject();
     jsonObj.put("action", 1);
     jsonObj.put("name", 2);


    List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("data", jsonObj.toString()));


    UrlEncodedFormEntity entityU = new UrlEncodedFormEntity(nvps);
    entityU.setContentEncoding(HTTP.UTF_8);
    //entityU.setContentType("application/json");
    httpost.setEntity(entityU);



    System.out.println("send about to do post");
    HttpResponse response = httpclient.execute(httpost);
    System.out.println("send post done");
    HttpEntity entity = response.getEntity();

    if (entity != null) {
        System.out.println("response.getStatusLine: " + response.getStatusLine());
        InputStream is = entity.getContent();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        while ( (line = br.readLine()) != null) {
            System.out.println("Response: " +  line);
            out = line;
        }
        is.close();

    } else {
        System.out.println("Response: response is null");
    }

} catch (Exception e) {
    e.getStackTrace();
}

if (httpclient != null) {
    httpclient.getConnectionManager().shutdown();
}

return out;     
}
于 2013-05-02T10:07:41.100 回答