php - 意外的状态行:在 HttpURLConnection 上使用 getInputStream() 时出现 Y������
问题描述
我正在尝试使用 HTTP 请求连接到 mysql 数据库。我创建了一个扩展 AsyncTask 广告的类,负责发出 http post 请求并检索数据。这是我的代码:
public class AttemptLogin extends AsyncTask {
String mUserName;
String mPassword;
String un = "admin01";
String pass = "admin01";
Connection con;
Context context;
AlertDialog dialog ;
public AttemptLogin(Context context, String mUserName, String Password) {
this.context = context;
this.mUserName = mUserName;
this.mPassword = Password;
}
@Override
protected void onPreExecute(){
dialog = new AlertDialog.Builder(context).create();
dialog.setTitle("Login Status");
}
protected void onPostExecute(String s){
dialog.setMessage(s);
dialog.show();
}
@Override
protected Object doInBackground(Object[] objects) {
try{
Log.d(tag, "Debut do in background");
String constr = "http://000.000.0.00:3306/login.php";
URL url = new URL(constr);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setDoInput(true);
http.setDoOutput(true);
http.setUseCaches(false);
http.setRequestMethod("POST");
http.setRequestProperty("Connection", "close");
OutputStream ops = http.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(ops,"UTF-8"));
String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(mUserName, "UTF-8") + " & " +
URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(mPassword, "UTF-8");
writer.write(data);
writer.flush();
writer.close();
ops.close();
String result = "";
String line = "";
InputStream ips = http.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ips,"ISO-8859-1"));
while ((line = reader.readLine()) != null){
result += line;
}
Log.d(tag, "Result = " + result);
reader.close();
ips.close();
http.disconnect();
onPostExecute(result);
return result;
}
catch (Exception e) {
Log.w("Error connection","" + e.getMessage());
e.printStackTrace();
}
return null;
}
}
当然,我放置的IP只是为了隐藏我的真实IP。无论如何,getInputStream() 处的行返回以下错误: Unexpected status line: Y������
我尝试了很多事情,包括将“连接”属性设置为“关闭”,但没有成功。
任何帮助将不胜感激,因为我已经被困了一段时间了。谢谢!
解决方案
您似乎正在尝试向运行 MySQL 特定协议的端口发出 HTTP 请求。请求发生,但响应不是 HTTP 响应,因此出现异常。
推荐阅读
- java - 尝试运行 jetty start.run 时出现 NullPointerException
- python - 如何使用具有分类特征的 RNN 嵌入层 - RecoSys 的分类任务
- amazon-ec2 - 收到错误消息“插件 [logsMetric] 中的错误:访问被拒绝。” 在 Cloudwatch 代理日志中
- c# - 如何从 C# 数据表在 MySql 中创建一个临时表?
- angular - Firebase Google 登录:此域无权运行此操作
- node.js - 计算nodeJs中的时间差
- python-3.x - 为什么只打印最后一个字母
- mpxj - 如何使用 MPXJ 库知道确切的 MS Project 版本
- swift - Braintree 支付集成:使用客户端授权令牌化密钥
- c# - 图表项目必须能够区分它的两个不同副本的哪些属性?