【问题标题】:Issue on POSTING data by using "NameValuePair"使用“NameValuePair”发布数据的问题
【发布时间】:2013-05-02 08:32:54
【问题描述】:

我用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' =&gt; 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}"

我的问题在哪里?

谢谢,

【问题讨论】:

  • 您是否使用过 Wireshark 来查看实际超出范围的内容?我怀疑你的 nvp 对象没有被正确序列化。
  • 我尝试只发送 json 数据,但是这样 POST 是空的

标签: java http-post httpclient


【解决方案1】:

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

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;     
}

【讨论】:

    【解决方案2】:

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

    如果您使用大量 http,那么您将在开发中获得良好的结果,因为您能够 CURL 您的所有表达式,以查看您在网络上的工作流程是否正常。如果您需要,您的单元测试可以包含仅发送大量 http 流量序列的 Bash 脚本...

    在 java 中,您需要找到一种方法来切换 http 日志,以便您的普通记录器可以编写类似于 this 的 HEADER/WIRE 语句--请参阅接受答案--

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

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

    read

    【讨论】:

      猜你喜欢
      • 2018-12-12
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      相关资源
      最近更新 更多