【问题标题】:Sending a specific POST Request to recieving an base64 format text file and displaying it at WebView发送特定的 POST 请求以接收 base64 格式的文本文件并在 WebView 中显示
【发布时间】:2018-08-22 09:52:51
【问题描述】:

我想就 POST 请求寻求帮助。
我想在服务器上发送一个特定的帖子,如果我将发送正确的身份验证,那么它将向我发送回 base64 格式的图像,然后我想在 WebView 中显示它。

我有 2 个 EditText 字段和一个按钮作为布局。
一个文本字段用于登录,第二个文本字段用于密码。 登录在消息正文中作为带有密钥用户名的 POST 参数发送。 密码使用 SHA-1 加密,并在名为授权的字段中的标头中发送。使用按钮,我可以发送应该如下所示的 Post 请求>

POST /download/image.php HTTP/1.1
授权:myPass
内容类型:application/x-www-form-urlencoded
主机:www.myweb.com

我的代码在这里。但它根本不起作用。我是新来的帖子请求,所以我真的不 知道如何正确编写它。

@Override
public void onClick(View arg0) {
  String login = mLoginView.getText().toString();
  String pass = mPasswordView.getText().toString();

  URL url = null;
  try {
      url = new URL("https://www.myweb.com/download/image.php");
  } catch (MalformedURLException e) {
      e.printStackTrace();
  }
  HttpURLConnection connection = null;

  try {

      if (login.length() == 0) {
          login = "correctpass";
      }
      if (pass.length() == 0) {
          pass = "correctlogin";
      }

      //calling getter for parsing the password into SHA1 hash
      try {
          pass = (String) HashTool.getSHA1Hash(pass);
      } catch (NoSuchAlgorithmException e) {
          e.printStackTrace();
      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      }

      connection = (HttpURLConnection) url.openConnection();
      connection.setRequestMethod("POST");
      connection.setRequestProperty("Key","username");
      DataOutputStream outputPost = new DataOutputStream(new BufferedOutputStream(connection.getOutputStream()));
      outputPost.write(login.getBytes());

      writeStream(outputPost);

      //this getBytes method won't work
      connection.setFixedLengthStreamingMode(outputPost.getBytes().length);
      outputPost.close();

      connection.setDoOutput(true);
  } catch (IOException e) {
      e.printStackTrace();

  } finally {

      if(connection != null) // Make sure the connection is not null.
          connection.disconnect();
  }

这是 writeStream() 方法。我不知道如何正确地做到这一点。

//method used for post request
private void writeStream(DataOutputStream out) throws IOException {
    String output = "something here??? maybe post ?";

    out.write(output.getBytes());
    out.flush();
}

【问题讨论】:

    标签: java android post base64 android-webview


    【解决方案1】:

    此方法在 link 上以 POST 的形式发送数据:

    private void executeLink(String link, String urlParameters) throws Exception{
    
    
        String response = null;
    
        byte[] postData       = urlParameters.getBytes( StandardCharsets.UTF_8 );
        int    postDataLength = postData.length;
        URL    url            = new URL( link );
        HttpURLConnection conn= (HttpURLConnection) url.openConnection();
        conn.setDoOutput( true );
        conn.setInstanceFollowRedirects( false );
        conn.setRequestMethod( "POST" );
        conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty( "charset", "utf-8");
        conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
        conn.setUseCaches( false );
    
    
        try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
            wr.write( postData );
        }
    
        conn.connect();
    
        if(conn.getResponseCode() == 200){
    
            //some thing else
        }else{
            //rejected 
        }
        return response;
    }
    

    调用方法:

    executeLink("url.link" , "param1=1&param2=val2&param2=val3")
    

    【讨论】:

    • 谢谢@Morteza。我将在 2 小时内尝试 cca。我去了市中心的科技图书馆,借了一些关于这个主题的书。我想了解更多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多