HttpURLConnection 只有本地主机中的原始数据
HttpURLConnection Only raw data in localhost
java 代码将原始数据插入 test.php,但它仅适用于本地主机 url,不适用于 public ip
我的url,
//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");
发送数据的代码,
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 "";
}
}
我的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"
?>
我在真实的 phone 中使用 3G 数据进行测试,并且...我的服务器以某种方式阻止了 public ip,因为当我使用本地 ip 时,它工作完美。阻塞写入输出流:urlConnection.getOutputStream()
我发现了问题:
问题出在我的DDNS记录类型上。
我使用记录类型 URL、协议 HTTP:// 和 IP X.X.X.X:1994
配置我的 ddns
我更改为记录类型 DNS 主机 (A),IPv4 地址:仅 myip X.X.X.X,没有端口。
在我的 URL 客户端中,我将端口设置在 URL.
我更改了 DDNS 的记录类型,并且,
http://myddns.net/test.php?username=user&password=pass
对此:
http://myddns.net:1994/test.php?username=user&password=pass
java 代码将原始数据插入 test.php,但它仅适用于本地主机 url,不适用于 public ip
我的url,
//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");
发送数据的代码,
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 "";
}
}
我的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"
?>
我在真实的 phone 中使用 3G 数据进行测试,并且...我的服务器以某种方式阻止了 public ip,因为当我使用本地 ip 时,它工作完美。阻塞写入输出流:urlConnection.getOutputStream()
我发现了问题:
问题出在我的DDNS记录类型上。
我使用记录类型 URL、协议 HTTP:// 和 IP X.X.X.X:1994
配置我的 ddns我更改为记录类型 DNS 主机 (A),IPv4 地址:仅 myip X.X.X.X,没有端口。
在我的 URL 客户端中,我将端口设置在 URL.
我更改了 DDNS 的记录类型,并且, http://myddns.net/test.php?username=user&password=pass 对此: http://myddns.net:1994/test.php?username=user&password=pass