我如何可以查看httppost发送的字符串? [英] How can I view the string sent from httppost?

查看:232
本文介绍了我如何可以查看httppost发送的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是试图张贴到一个PHP脚本在本地服务器上code片段。我将数据发回给服务器的MySQL表和数据量是非常小的,我想我会使用的HttpClient嵌入的URL参数的数据。

不过,即使我从服务器获取响应OK(HTTP code = 200),很显然,服务器端PHP脚本没有得到正确格式的数据(上有没有影响MySQL表)。然而,当我手动像这样一个浏览器输入网址+ ARGS:

<$p$p><$c$c>http://10.0.0.13/jobs_returndata_test.php?jobnum=189193&pnum=3&entime=13:00&extime=14:00

例如,一切工作正常(的PHP脚本正确地写入到MySQL表)。

我的问题:有没有一种方法,我居然可以查看哪些是由HttpClient的方式吗? (也就是说,整个的ToString的HttpPost对象的内容是什么?)。下面是Android的端片段:

  //创建一个新的HttpClient和邮政头,发送数据,args设置为PHP文件
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(http://10.0.0.13/jobs_returndata.php);
尝试{
    清单&LT;&的NameValuePair GT; namevaluepairs中=新的ArrayList&LT;&的NameValuePair GT;();
    nameValuePairs.add(新BasicNameValuePair(jobnum,convertSimple(jobNum)));
    nameValuePairs.add(新BasicNameValuePair(PNUM,convertSimple(的rowNum)));
    nameValuePairs.add(新BasicNameValuePair(entime,enteredInTime));
    nameValuePairs.add(新BasicNameValuePair(extime,enteredOutTime));    httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
    Log.i(HTTP POST,执行后发送jobnum =+ convertSimple(jobNum)+
    与&amp; PNUM =+ convertSimple(的rowNum)+与&amp; entime =+ enteredInTime
                    +与&amp; extime =+ enteredOutTime);
    //执行HTTP POST请求
    HTT presponse响应= httpclient.execute(httppost);
    Log.i(HTTP POST+ response.getStatusLine()。getReasonPhrase()+code =+ convertSimple(response.getStatusLine()的getStatus code从服务器=回应()) );        }赶上(ClientProtocolException E){
            Log.e(HTTP POST,协议错误=+ e.toString());
        }赶上(IOException异常五){
            Log.e(HTTP POST,IO错误=+ e.toString());
        }

和我的服务器端PHP文件是:

 ?PHP
$ jobnum = $ _ GET ['jobnum'];
//回声jobnum = $ jobnum&LT; BR /&gt;中;
$ PNUM = $ _ GET ['PNUM'];
//回声PNUM = $ PNUM&LT; BR /&gt;中;$ entime = $ _ GET ['entime'];
//回声entime = $ entime&LT; BR /&gt;中;$ extime = $ _ GET ['extime'];
//回声extime = $ extime&LT; BR /&gt;中;
$胸径= mysql_connect(localhost的,史蒂夫,撑着);
mysql_select_db(DriverJobs);$结果= mysql_query(更新作业SET EntryTime ='$ entime'WHERE jobnumber可以='$ jobnum'AND PointNum ='$ PNUM');
$结果= mysql_query(更新作业SET EXITTIME ='$ extime'WHERE jobnumber可以='$ jobnum'AND PointNum ='$ PNUM');//回声做&LT; BR /&gt;中;
?&GT;

对于相关板块的logcat的:


  

13 06-22:33:11.341:I / HTTP POST(27788):执行邮寄
  jobnum = 189193&放大器; PNUM = 3及entime = 9:00安培; extime = 9:5月6日至22日13:33:11.581:
  I / HTTP POST(27788):从服务器响应= OK code = 200


权限都不错(在INTERNET清单文件,并为PHP文件,是的Apache2和运行时,API = 8,使用Eclipse与Android模拟器)。的 convertSimple()的是一个2班轮返回字符串相当于一个int原始的。

预先感谢在看这个!


解决方案

您看到的变量,当你在浏览器中输入他们的理由是GET和POST之间的区别。当您使用您发送您的变量GET格式的浏览器。

在你的php code使用:

  $ _ POST ['varname的'];

或者你可以使用请求来获取POST和GET增值经销​​商:

  $ _ REQUEST ['varname的'];

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!

解决方案

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'];

这篇关于我如何可以查看httppost发送的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆