我有一个Android应用程序,它将发布数据发送到服务器,这是我的代码:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Server.currentServer + "/mainmenu_mobile.aspx");
try {
menuItems = null;
boolean status = false;
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("auth", Util.Token()));
nameValuePairs.add(new BasicNameValuePair("AppID", Util.AppID));
nameValuePairs.add(new BasicNameValuePair("locale", Locale.getDefault().getISO3Language()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 201)
{
String result = convertStreamToString(response.getEntity().getContent());
InputStream inStream = response.getEntity().getContent();
menuItems = new ArrayList<MenuObject>();
parser.parse(inStream);
}
try
{
response.getEntity().consumeContent();
}
catch (Exception g)
{
}
log.i(response.getStatusLine().getStatusCode()+"");
status = true;
return status;
} catch (ClientProtocolException e) {
e.printStackTrace();
// TODO Auto-generated catch block
return false;
} catch (IOException e) {
e.printStackTrace();
// TODO Auto-generated catch block
return false;
}
我应该如何在c#中的服务器上阅读这些参数?我需要仔细阅读它们中的每一个。还有什么方法可以查看用post发送的数据的值,以确保这些数据是正确的? 到目前为止,这是mainmenu_mobile.aspx:
using ClassLibrary1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TiboServer_2;
using System.Data.SqlClient;
using System.Xml;
public partial class mainmenu_mobile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Util util = new Util();
Dictionary<String, String> postParams = util.checkHeader(Request, new String[] { "auth", "AppID", "locale" });
util.check(Request.UserAgent, constant.PAGECODE_MAINMENU, Request.UserHostAddress);
if (util.RESPONSE == constant.RESPONSE_ACCESS_GRANTED && util.STATE == constant.STATE_INSERTED_SUCCESFULY)
{
Dictionary<string, string> Params = new Dictionary<string, string>();
Params.Add("@username", util.username);
Params.Add("@locale", postParams["locale"]);
String sql = "select title, url, icon, menucode from mobilemenu where locale = '" + postParams["locale"] + "' order by position asc FOR XML RAW ('menu'), ROOT ('mainmenu'), ELEMENTS";
//xtr.WhitespaceHandling = System.Xml.WhitespaceHandling.None;
XmlDocument doc = db_Action.getXmlDocument(sql,Params);
if (doc != null)
{
Response.ContentType = "text/xml"; //must be 'text/xml'
Response.ContentEncoding = System.Text.Encoding.UTF8; //we'd like UTF-8
doc.Save(Response.Output); //save to the text-writer
}
}
else
{
Response.StatusCode = 403;
}
Response.End();
}
}
和Util.cs:
using System;
using System.Collections.Generic;
using System.Web;
using ClassLibrary1;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for Util
/// </summary>
///
namespace TiboServer_2
{
public class Util
{
//public static String loggingtable = "Server=tcp:jmyvkj5sxx.database.windows.net,1433;Database=tibonalytics_db;User ID=TiboDb@jmyvkj5sxx;Password=Tibo2015;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;";
public int RESPONSE = constant.RESPONSE_NO_RESPONSE;
public int STATE = constant.STATE_BEGIN;
public String username = "";
public String appid = "-1";
public String App_id;
public String token;
private String passwordkey = "";
public Dictionary<String,String> checkHeader(HttpRequest Request,String[] Params)
{
Dictionary<String, String> postParameters = new Dictionary<String, String>();
foreach (String param in Params)
{
if (Request.Form[param] == null)
{
return null;
}
postParameters.Add(param, Request.Form[param]);
}
App_id = postParameters["AppID"];
token = postParameters["auth"];
return postParameters;
}
public Dictionary<String, String> checkHeader(HttpRequest Request, String[] Params,Boolean[] optional)
{
Dictionary<String, String> postParameters = new Dictionary<String, String>();
for(int i=0;i<Params.Length;i++)
{
String param = Params[i];
if (Request.Form[param] == null && !optional[i])
{
return null;
}
postParameters.Add(param, Request.Form[param]);
}
App_id = postParameters["AppID"];
token = postParameters["auth"];
return postParameters;
}
public Dictionary<String, String> checkHeader(HttpRequest Request, String[] Params, int[] Validate)
{
Dictionary<String, String> postParameters = new Dictionary<String, String>();
for (int i = 0; i < Params.Length; i++)
{
String param = Params[i];
if (Request.Form[param] == null)
{
return null;
}
else if (Validate[i] == validate.INTEGER)
{
try
{
Convert.ToInt32(param);
}
catch
{
return null;
}
}
else if (Validate[i] == validate.DOUBLE)
{
try
{
Convert.ToDouble(param);
}
catch
{
return null;
}
}
postParameters.Add(param, Request.Form[param]);
}
App_id = postParameters["AppID"];
token = postParameters["auth"];
return postParameters;
}
public Dictionary<String, String> checkHeader(HttpRequest Request, String[] Params, int[] Validate,Boolean[] optional)
{
Dictionary<String, String> postParameters = new Dictionary<String, String>();
for (int i = 0; i < Params.Length; i++)
{
String param = Params[i];
if (Request.Form[param] == null && !optional[i])
{
return null;
}
else if (Validate[i] == validate.INTEGER)
{
try
{
Convert.ToInt32(param);
}
catch
{
return null;
}
}
else if (Validate[i] == validate.DOUBLE)
{
try
{
Convert.ToDouble(param);
}
catch
{
return null;
}
}
postParameters.Add(param, Request.Form[param]);
}
App_id = postParameters["AppID"];
token = postParameters["auth"];
return postParameters;
}
public void check(String UserAgent, int pageCode, String IP)
{
Encryption ee = new Encryption();
String Token = "";
try
{
appid = ee.Decrypt(App_id, constant.AppID_Key);
}
catch (Exception f)
{
RESPONSE = constant.RESPONSE_UNABLE_TO_DECRYPT_APPID;
return;
}
try
{
Token = ee.Decrypt(token, constant.EK);
}
catch (Exception f)
{
RESPONSE = constant.RESPONSE_UNABLE_TO_DECRYPT_TOKEN;
return;
}
string[] Token_Content = Token.Split(';');
username = System.Web.HttpUtility.UrlDecode(Token_Content[0].Trim());
String password = System.Web.HttpUtility.UrlDecode(Token_Content[1].Trim());
String epochetime = System.Web.HttpUtility.UrlDecode(Token_Content[2].Trim());
try
{
username = ee.Decrypt(username, constant.UsernameKey);
}
catch
{
RESPONSE = constant.RESPONSE_UNABLE_TO_DECRYPT_USERNAME;
return;
}
try
{
epochetime = ee.Decrypt(epochetime, constant.TimeKey);
}
catch
{
RESPONSE = constant.RESPONSE_UNABLE_TO_DECRYPT_EPOCHTIME;
return;
}
Dictionary<String,String> Params = new Dictionary<String,String>();
Params.Add("@username", username);
String query;
if (appid == "1")
{
query = "select videokey,id from login where username = @username and lockaccount = 0";
}
else
{
query = "select mobilevideokey,id from login where username = @username and lockaccount = 0";
}
DataTable dt = db_Action.ExecuteQuery(query, Params);
if (dt.Rows.Count > 0)
{
passwordkey = dt.Rows[0][0].ToString(); //videokey
}
long id = (int)dt.Rows[0][1]; //id
if (username == "promo")
{
RESPONSE = constant.RESPONSE_ACCESS_GRANTED;
STATE = constant.STATE_INSERTED_SUCCESFULY;
}
else
{
try
{
password = ee.Decrypt(password, passwordkey);
}
catch
{
RESPONSE = constant.RESPONSE_UNABLE_TO_DECRYPT_PASSWORD;
return;
}
RESPONSE = constant.RESPONSE_ACCESS_GRANTED;
Params.Add("@code",appid + "_" + pageCode + "_" + id + "_" + epochetime);
String query = "insert into Monitor (code) Values(@code)";
try
{
db_Action.ExecuteSingleQuery(query, Params);
STATE = constant.STATE_INSERTED_SUCCESFULY;
}
catch
{
STATE = constant.STATE_DUBLICATE_CALL;
Params.Clear();
Params.Add("@username", username);
Params.Add("@code", appid + "_" + pageCode + "_" + id + "_" + epochetime + "_" + (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds + "_" + IP);
Params.Add("@value",UserAgent);
query = "insert into Monitor (code,value) Values(@code,@value)";
db_Action.ExecuteSingleQuery(query, Params);
}
}
}
}
}