与非.com网站的HttpURLconnection连接失败

时间:2019-06-02 17:49:38

标签: java android hosting

我一直试图将我的Android应用程序连接到php服务器。我进行了php服务器回显"Connection success",以便我可以检索相同的文本并将其显示在Alertbox中android应用程序。我有一个带有.cf的网站,并且每次我连接时该应用程序都会关闭。 我检查代码发现没有问题,然后尝试访问.com网站,该应用程序成功检索到了页面上显示的内容的html形式。是否存在阻止HttpURLconnection形式的内容连接到非.com网站。

这是代码。

public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx) {
    context = ctx;
}//takes context as an argument
@Override
protected String doInBackground(String... params) {
    String type = params[0];
    String login_url = "http://www.thejoint.cf/test.php";
    if(type.equals("login")) {
        try {
            String user_name = params[1];
            String password = params[2];
            URL url = new URL(login_url);//what does the url object have
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
                    +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
            String result="";
            String line="";
            while((line = bufferedReader.readLine())!= null) {
                result+=line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

@Override
protected void onPreExecute() {
    alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setTitle("Login Status");
}

@Override
protected void onPostExecute(String result) {
    alertDialog.setMessage(result);
    Log.i("info",result);
    alertDialog.show();
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}

}

1 个答案:

答案 0 :(得分:0)

  

那么有什么办法可以阻止HttpURLconnection表单连接到非.com网站吗?

您创建一个正则表达式:

String expression  =  "[^"]+\.com" ; 

并测试您的网址是否与之匹配:

if(your_url.matches(expression)){

 // your url end with .com , do what you want 

}else{

 // your url doesn't end with .com , do what you want

}