Php 本地主机中仅HttpURLConnection原始数据
java代码将原始数据插入test.php,但它仅适用于localhost url,而不适用于公共ip 我的网址Php 本地主机中仅HttpURLConnection原始数据,php,android,httpurlconnection,Php,Android,Httpurlconnection,java代码将原始数据插入test.php,但它仅适用于localhost url,而不适用于公共ip 我的网址 //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/tes
//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"
?>
//在test.php中
我使用3G数据在真实手机上进行测试,然后。。。我的服务器在某种程度上阻塞了公共ip,因为当我使用本地ip时,它工作得非常好。阻止写入输出流:urlConnection.getOutputStream()我发现了问题:
问题是我的DDN记录类型
我使用记录类型URL、协议HTTP://和IP X.X.X.X:1994配置我的DDN
我更改为记录类型DNS主机(A),IPv4地址:仅myip X.X.X.X,不带端口
在我的URL客户端中,我在URL中设置端口
我更改了DDN的记录类型,
为此:
//此URL不起作用,输出:phpOK
?如果输出是phpOK,那么我会说它是有效的。因为,原始数据没有显示任何内容。输出为:echo file\u get\u contents('php://input“)+“phpOK”;你以错误的方式寻求帮助。我正在使用Android中的HTTPConnection将数据发布到php服务器。问题是,仅适用于本地url 192.168.1.100,而不适用于我的全局ip,如注意:客户端和服务器在同一路由器中连接,客户端和服务器在同一路由器中连接???你说你的服务器在互联网上。你的便条没有意义。很好,你找到了!感谢您帮助@greenapps