爱玺玺

爱玺玺的生活日记本。wx:lb87626

php模拟浏览器form表单post数据x-www-form-urlencoded,curl模拟向java servlet发送数据

$postUrl = 'http://localhost:8080/mydouyin/servlet/GetGorgon'; 

$postData = array( 

    'user'=>"1111111111", 

    'cookie'=>"222222" 

); 

$postData = http_build_query($postData); 

$curl = curl_init(); 

curl_setopt($curl, CURLOPT_URL, $postUrl); 

curl_setopt($curl, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15'); 

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate 

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  

curl_setopt($curl, CURLOPT_POST, true); 

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); 

curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); 

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 

$r = curl_exec($curl);  

curl_close($curl); 

//phpfensi.com 

print_r($r); 



servlet主要代码:

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

MyUtils myUtils = new MyUtils();

System.out.println(request.getParameter("cookie"));

if(request.getParameter("cookie")!=null){

myUtils.writeFile("F:/apache-tomcat-7.0.70-windows-x64/apache-tomcat-7.0.70/webapps/mydouyin/cookie.txt", request.getParameter("cookie"));

}

request.setCharacterEncoding("UTF-8");

response.setContentType("text/html;charset=utf-8");

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

out.println("<HTML>");

out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");

out.println("  <BODY>");

out.print("    This is 返回POST数据"+request.getParameter("cookie"));

System.out.println("    This is 返回POST数据"+request.getParameter("cookie"));

//out.print(this.getClass());

//out.println(", using the POST method");

out.println("  </BODY>");

out.println("</HTML>");

out.flush();

out.close();

}


发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5