Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Android中使用HttpURLConnection从asp发送和检索数据?_C#_Android_Asp.net_Httpurlconnection - Fatal编程技术网

C# 如何在Android中使用HttpURLConnection从asp发送和检索数据?

C# 如何在Android中使用HttpURLConnection从asp发送和检索数据?,c#,android,asp.net,httpurlconnection,C#,Android,Asp.net,Httpurlconnection,我在android中有一个登录过程,数据库中有一个用户帐户信息和一个aspx文件,可以检查用户名和密码是否正确 在我的aspx.cs文件中,我有一个过程方法来检查用户名和密码,如下所示: public void ProcessRequest(HttpContext context) { HttpRequest request = context.Request; HttpResponse response = context.Response; System.Web.

我在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#
未完成

   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")+
        "&param2="+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?请提供帮助。

在将数据写入outputstream后检查Http响应代码值

//Write Data
StringBuilder builder = new StringBuilder();
            builder.append(conn.getResponseCode())
                   .append(" ")
                   .append(conn.getResponseMessage())
                   .append("\n");
// Read response data

您已经发布了您的参数。那你为什么要问?你的脚本应该返回一个类似“succes”或“failed”的文本,而不是布尔值。我不知道我的urlParameters是如何工作并应用于C#的,但为什么你不告诉我是否存在连接?或者如果有陷阱?以及您得到的响应。toString()?你应该告诉我们,因为我们怎么知道这些呢?
newurl(“localhost:60068/SampleLogin.aspx”)。您忘记告诉协议:
新URL(“http://localhost:60068/SampleLogin.aspx");。此外,不能使用localhost作为主机名。请告诉你的服务器在哪里运行,以及你是否使用仿真器或Android设备。在C代码中,你可以看到登录、用户名和密码等参数。但在Android代码中,您只发送param1和param2。