我在java方法中写道,负责发送像POST
ajax
public String getWebInfo() {
DefaultHttpClient httpclient = null;
String out = null;
try {
System.out.println("send started");
httpclient = new DefaultHttpClient();
Credentials cred = new UsernamePasswordCredentials("user", "password");
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
cred);
HttpPost httpost = new HttpPost("https://page/php/data.ajax.php");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("action", "1"));
nvps.add(new BasicNameValuePair("name", "2"));
UrlEncodedFormEntity entityU = new UrlEncodedFormEntity(nvps);
entityU.setContentEncoding(HTTP.UTF_8);
//entityU.setContentType("application/json");
httpost.setEntity(entityU);
System.out.println("send about to do post");
HttpResponse response = httpclient.execute(httpost);
System.out.println("send post done");
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("response.getStatusLine: " + response.getStatusLine());
InputStream is = entity.getContent();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null) {
System.out.println("Response: " + line);
out = line;
}
is.close();
} else {
System.out.println("Response: response is null");
}
} catch (Exception e) {
e.getStackTrace();
}
if (httpclient != null) {
httpclient.getConnectionManager().shutdown();
}
return out;
}
输出为空:
send started
send about to do post
send post done
response.getStatusLine: HTTP/1.1 200 OK
Response: []
服务器端
来自msqLogFile("page/post",Array('post' => urldecode(implode(", ", $_POST))));
我只获得没有密钥的数据:
"2013-05-02 04:21:09","1,2"
它应该是json数据:
来自tail -f /etc/httpd/logs/ssl_request_log
82.80.25.130 TLSv1 AES128-SHA "POST /page/php/data.ajax.php HTTP/1.1" 484
82.80.25.130 TLSv1 AES128-SHA "POST /page/php/data.ajax.php HTTP/1.1" 3
当我取消注释entityU.setContentType("application/json");
时,我根本没有数据。
从javascript一切正常:
var json = JSON.stringify({action: 1,name: 2});
$.ajax({
type: "POST",
url: "/page/php/data.ajax.php",
dataType:"json",
data:{data:json},
success:function(data){
在这里我得到回应:
"2013-05-02 04:21:09","{"action":1,"name":2}"
我的问题在哪里?
谢谢,
答案 0 :(得分:0)
我找到了解决方法,但假设有人会找到更好的回应:
public String getWebInfo() {
DefaultHttpClient httpclient = null;
String out = null;
try {
System.out.println("send started");
httpclient = new DefaultHttpClient();
Credentials cred = new UsernamePasswordCredentials("user", "password");
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
cred);
HttpPost httpost = new HttpPost("https://page/php/data.ajax.php");
JSONObject jsonObj = new JSONObject();
jsonObj.put("action", 1);
jsonObj.put("name", 2);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("data", jsonObj.toString()));
UrlEncodedFormEntity entityU = new UrlEncodedFormEntity(nvps);
entityU.setContentEncoding(HTTP.UTF_8);
//entityU.setContentType("application/json");
httpost.setEntity(entityU);
System.out.println("send about to do post");
HttpResponse response = httpclient.execute(httpost);
System.out.println("send post done");
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("response.getStatusLine: " + response.getStatusLine());
InputStream is = entity.getContent();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null) {
System.out.println("Response: " + line);
out = line;
}
is.close();
} else {
System.out.println("Response: response is null");
}
} catch (Exception e) {
e.getStackTrace();
}
if (httpclient != null) {
httpclient.getConnectionManager().shutdown();
}
return out;
}
答案 1 :(得分:0)
如果您使用了很多http,那么您可以获得很好的结果,因为您无法对所有表达式进行CURL,以便查看您的网络工作流程是否正常。如果需要,您的单元测试可以包含仅发送大量http流量的Bash脚本......
在java中,您需要找到一种方法来切换http日志,以便您的普通记录器可以编写看起来像this的HEADER / WIRE语句 - 请接受ans -
您将清楚地看到服务器的所有标题TO和FRM,WIRE上的所有正文信息,以及显示方向“&lt;&lt;”的服务器或“&gt;&gt;”在您的正常记录器中。
弄清楚怎么做很棘手,但是当你弄明白的时候,值得一读IMO。