How can I view the string sent from httppost?

2019-07-19 17:54发布

Below is a snippet of code that is trying to post to a PHP script on a local server. I'm transmitting data back to the server's MySQL table and as the amount of data is very small, I thought I'd use HttpClient to embed the data in the URL arguments.

However, even though the response I'm getting from the server is OK (HTTP code = 200), it's clear that the server-side PHP script is not getting correctly formatted data (there is no effect on the MySQL table). However, when I manually enter the URL+args in a browser like so:

http://10.0.0.13/jobs_returndata_test.php?jobnum=189193&pnum=3&entime=13:00&extime=14:00 

for example, everything works fine (the PHP scripts writes correctly to the MySQL table).

My question: Is there a way I can actually view what is being sent by HttpClient? (ie, the entire .toString contents of the HttpPost object?). Here's the Android-side snippet:

// Create a new HttpClient and Post Header, send data as args to PHP file
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.0.13/jobs_returndata.php");
try {
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("jobnum",convertSimple(jobNum)));
    nameValuePairs.add(new BasicNameValuePair("pnum",convertSimple(rowNum)));
    nameValuePairs.add(new BasicNameValuePair("entime",enteredInTime));
    nameValuePairs.add(new BasicNameValuePair("extime",enteredOutTime));

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    Log.i("HTTP Post", "Execute Post sending jobnum="+ convertSimple(jobNum) +      
    "&pnum="+ convertSimple(rowNum) + "&entime=" + enteredInTime
                    + "&extime=" + enteredOutTime);
    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);
    Log.i("HTTP Post", "Response from server = " + response.getStatusLine().getReasonPhrase() + "  Code = " + convertSimple(response.getStatusLine().getStatusCode()));

        } catch (ClientProtocolException e) {
            Log.e("HTTP Post", "Protocol error = " + e.toString());
        } catch (IOException e) {
            Log.e("HTTP Post", "IO error = " + e.toString());
        }

And my server-side PHP file is:

?php
$jobnum=$_GET['jobnum'];
//echo "jobnum = $jobnum <br />";
$pnum=$_GET['pnum'];
//echo "pnum = $pnum <br />";

$entime=$_GET['entime'];
//echo "entime = $entime <br />";

$extime=$_GET['extime'];
//echo "extime = $extime <br />";


$dbh=mysql_connect ("localhost", "Steve", "pluggin");
mysql_select_db ("DriverJobs");

$result = mysql_query("UPDATE jobs SET EntryTime = '$entime' WHERE JobNumber = '$jobnum' AND PointNum = '$pnum' ");  
$result = mysql_query("UPDATE jobs SET ExitTime = '$extime' WHERE JobNumber = '$jobnum' AND PointNum = '$pnum' ") ;  

//echo "done <br />";
?>

The logcat for the relevant section:

06-22 13:33:11.341: I/HTTP Post(27788): Execute Post sending jobnum=189193&pnum=3&entime=9:00&extime=9:05 06-22 13:33:11.581: I/HTTP Post(27788): Response from server = OK Code = 200

Permissions are good (in the Manifest file for INTERNET, and for the PHP file, Apache2 is on and running, API=8, using Eclipse with Android emulator). convertSimple() is a 2 liner that returns the String equivalent for an int primitive.

Thanks in advance for looking at this!

4条回答
唯我独甜
2楼-- · 2019-07-19 18:16

Use this method

private String convertStreamToString(InputStream is) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }
    is.close();
    return sb.toString();
}   

And call like this to see what entities are included in your post method

try {
    HttpPost postMethod = new HttpPost("your url");
    Log.i(TAG,"HTTP Entiry : " + convertStreamToString(postMethod.getEntity().getContent()));
}catch (Exception e){
    e.printStackTrace();
}
查看更多
贪生不怕死
3楼-- · 2019-07-19 18:22

The reason you see the variables when you enter them in the browser is the difference between GET and POST. When you use the browser you are sending your variables in GET format.

In your php code use:

$_POST['varname'];

Or you can use request to get POST and GET vars:

$_REQUEST['varname'];
查看更多
Summer. ? 凉城
4楼-- · 2019-07-19 18:30

after HttpResponse add code as shown below

       InputStream is;

           try{
             if( response == null ){

               }else{
                  HttpEntity entity = response.getEntity();
                  is = entity.getContent();   
                     }

              } catch (Exception e) {
               Log.e("log_tag", "Error in http connection " + e.toString());
                 }

        // convert response to string
    try {
          BufferedReader reader = new BufferedReader(new InputStreamReader(
              is, "iso-8859-1"));
          StringBuilder sb = new StringBuilder();
          String line = null;
          while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
          }
          is.close();
          result = sb.toString();
          Log.e("res",result); 
      } catch (Exception e) {
          Log.e("log_tag", "Error converting result " + e.toString());
       }

where result will give u response in String i have printed in Log for testing purpose

查看更多
看我几分像从前
5楼-- · 2019-07-19 18:32

I would use fiddler to view the request

查看更多
登录 后发表回答