【发布时间】: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