我在android中有一个登录过程,一个数据库中的用户帐户信息和一个aspx文件,可以检查用户名和密码是否正确。
在我的aspx.cs文件中,我有一个进程方法来检查用户名和密码,如下所示:
public void ProcessRequest(HttpContext context)
{
HttpRequest request = context.Request;
HttpResponse response = context.Response;
System.Web.SessionState.HttpSessionState session = context.Session;
string result = "";
try
{
string action = request.Form["action"].ToString();
switch (action)
{
//Login test
case "login":
result = this.login(session, request);
break;
}
}
catch (Exception e)
{
result = "{\"message\":\"" + e.Message + "\"}";
}
response.Clear();
response.ContentType = "application/json;charset=UTF-8;";
response.Write(result);
response.End();
}
#region Login test
private string login(System.Web.SessionState.HttpSessionState session, HttpRequest request)
{
LoginSample sample = new LoginSample();
JsonObject jo = new JsonObject();
string username = request.Form["username"];
string password = request.Form["password"];
bool login = sample.Login(username, password);
if (login)
jo.Add("msg", "success");
else
jo.Add("msg", "failed");
return jo.ToString();
}
我的班级将用户名和密码传递给C#INCOMPLETE
:
public static String excutePost(String vUserName, String vPassword)
{
try {
URL url = new URL("localhost:60068/SampleLogin.aspx");
String urlParameters="param1=" + URLEncoder.encode(vUserName,"UTF-8")+
"¶m2="+URLEncoder.encode(vPassword,"UTF-8");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setFixedLengthStreamingMode(urlParameters.getBytes().length);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.writeBytes(urlParameters);
dataOutputStream.flush();
dataOutputStream.close();
InputStream inputStream = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuffer response = new StringBuffer();
while((line = bufferedReader.readLine()) != null)
{
response.append(line);
response.append('\r');
}
bufferedReader.close();
return response.toString();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
如何将参数传递给aspx.cs文件并处理并将布尔值返回给android?请帮忙。
答案 0 :(得分:0)
将数据写入输出流后检查您的Http响应代码值。
//Write Data
StringBuilder builder = new StringBuilder();
builder.append(conn.getResponseCode())
.append(" ")
.append(conn.getResponseMessage())
.append("\n");
// Read response data