dongtang4019 2016-11-12 05:16
浏览 179
已采纳

HttpURLConnection只有localhost中的原始数据

The java code insert raw data into test.php, but it only works with localhost url, and don't with public ip

My urls,

//this URL work perfect, output: {"a":"b"} phpOK
postJSON("http://192.168.1.100/test.php?username=user&password=pass");

//this URL don't work, output: phpOK
postJSON("http://myddns.net/test.php?username=user&password=pass");

Code to send data,

public String postJSON(String URL, String jsonStr) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    HttpURLConnection urlConnection = null;
    StringBuffer response = null;
    try {
        URL url = new URL(URL);
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestProperty("Content-Type", "application/json");
        urlConnection.setRequestMethod("POST");
        urlConnection.setFixedLengthStreamingMode(jsonStr.getBytes().length);

        urlConnection.setConnectTimeout(5000);
        urlConnection.setReadTimeout(5000);
        urlConnection.setUseCaches(false);
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.connect();

JSONObject jsonStr= new JSONObject();
jsonStr.put("a", "b");

        DataOutputStream printout = new DataOutputStream(urlConnection.getOutputStream());
        printout.writeBytes(jsonStr);
        printout.flush();
        printout.close();

        //if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
        InputStream is = urlConnection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        String inputLine;
        response = new StringBuffer();

        while ((inputLine = br.readLine()) != null) {
            response.append(inputLine);
        }
        br.close();
    } finally {
        if(urlConnection != null)
            urlConnection.disconnect();
        if(response != null)
            return response.toString();
        else
            return "";
    }
}

My PHP,

//in test.php
<?php
 $phpInput = file_get_contents('php://input');
 echo $phpInput." phpOK";
if(isset($_GET['username']) & isset($_GET['password']))
 echo "extra vars catched"
?>

I test in a real phone using 3G data and... My server is blocking in some way the public ip, because when I use the local ip it's work perfect. Blocking the writing output stream: urlConnection.getOutputStream()

  • 写回答

1条回答

  • dounuo9921 2016-11-15 22:34
    关注

    I found the problem:

    The problem is my record type of DDNS.

    I config my ddns with record type URL, protocol HTTP:// and IP X.X.X.X:1994

    I change to record type DNS Host (A), IPv4 Address: myip only X.X.X.X , without port.

    In my URL client, I set the port in the URL.

    I change the record type of my DDNS and, http://myddns.net/test.php?username=user&password=pass to this: http://myddns.net:1994/test.php?username=user&password=pass

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突