非FORM格式,使用POST方式向servlet发送请求
2012-08-05 11:51
726 查看
近期项目,合作方所提供的数据通信API,采用servlet服务端doPost()接收各项请求参数;双方系统交互(请求/响应)均由后台逻辑自动完成,即不通过FORM提交请求方式实现,因此后台处理逻辑需模拟HTTP请求并使用POST方式向对方发送数据。
因在系统中常用,略为笔记,PHP模拟POST请求代码如下:
$host ="202.112.xxx.xxx";
$port =8099;
$path ='/servicepath';
$cmd = "BALANCE";
$userName="test";
//Build Request String
$xml = '<?xml version="1.0"?>' ;
$xml .= '<eload>' ;
$xml .= '<command>'.$cmd .'</command>' ;
$xml .= '<userid>'.$userName.'</userid>' ;
$xml .= '</eload>' ;
$errno = '' ;
$errstr = '' ;
$time = date('His') ;
//Create Socket
$fp = fsockopen( $host, $port, $errno, $errstr, 10 ) or exit( $errstr."--->".$errno ) ;
$length = strlen( $xml ) ;
//Create Http Header with Post Method
$header = "POST http://".$host.":".$port.".$path."HTTP/1.1\r\n" ;
$header .= "Accept: */*\r\n" ;
$header .= "Accept-Language: zh-cn\r\n" ;
$header .= "Content-Type: text/xml; charset=ISO-8859-1\r\n" ;
$header .= "Accept-Encoding: gzip, deflate\r\n" ;
$header .= 'User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; ';
$header .= 'Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; ';
$header .= "NET CLR 3.0.30729; Media Center PC 6.0)\r\n" ;
$header .= "Host: ".$host.":".$port."" ;
$header .= "Content-Length: ".$length."\r\n" ;
$header .= "Connection: Keep-Alive\r\n" ;
$header .= "Pragma: no-cache\r\n\r\n" ;
//Link Request data
$header .= $xml."\r\n" ;
//Send Request
fputs( $fp, $header) ;
$inheader = 1 ;
while( !feof($fp) ){
$line = fgets( $fp, 1024 ) ;
//Get Response
if( $inheader && ($line == "\n" || $line == "\r\n") ){
$inheader = 0 ;
}
if($inheader == 0){
//echo $line ;
$result = $line;
}
}
//Clost Socket
fclose( $fp ) ;
相信仍有不少童鞋在此处困惑,特共享之~
Sky
343025026
因在系统中常用,略为笔记,PHP模拟POST请求代码如下:
$host ="202.112.xxx.xxx";
$port =8099;
$path ='/servicepath';
$cmd = "BALANCE";
$userName="test";
//Build Request String
$xml = '<?xml version="1.0"?>' ;
$xml .= '<eload>' ;
$xml .= '<command>'.$cmd .'</command>' ;
$xml .= '<userid>'.$userName.'</userid>' ;
$xml .= '</eload>' ;
$errno = '' ;
$errstr = '' ;
$time = date('His') ;
//Create Socket
$fp = fsockopen( $host, $port, $errno, $errstr, 10 ) or exit( $errstr."--->".$errno ) ;
$length = strlen( $xml ) ;
//Create Http Header with Post Method
$header = "POST http://".$host.":".$port.".$path."HTTP/1.1\r\n" ;
$header .= "Accept: */*\r\n" ;
$header .= "Accept-Language: zh-cn\r\n" ;
$header .= "Content-Type: text/xml; charset=ISO-8859-1\r\n" ;
$header .= "Accept-Encoding: gzip, deflate\r\n" ;
$header .= 'User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; ';
$header .= 'Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; ';
$header .= "NET CLR 3.0.30729; Media Center PC 6.0)\r\n" ;
$header .= "Host: ".$host.":".$port."" ;
$header .= "Content-Length: ".$length."\r\n" ;
$header .= "Connection: Keep-Alive\r\n" ;
$header .= "Pragma: no-cache\r\n\r\n" ;
//Link Request data
$header .= $xml."\r\n" ;
//Send Request
fputs( $fp, $header) ;
$inheader = 1 ;
while( !feof($fp) ){
$line = fgets( $fp, 1024 ) ;
//Get Response
if( $inheader && ($line == "\n" || $line == "\r\n") ){
$inheader = 0 ;
}
if($inheader == 0){
//echo $line ;
$result = $line;
}
}
//Clost Socket
fclose( $fp ) ;
相信仍有不少童鞋在此处困惑,特共享之~
Sky
343025026
相关文章推荐
- AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式
- Android开发使用POST方式向服务器请求和发送数据
- JavaWeb 发送post请求的2种方式(form、json)
- 浅谈使用Fiddler工具发送post请求(带有json数据)以及get请求(Header方式传参)
- AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式
- AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式
- [转]AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式
- AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式
- 使用ajax发送post请求后调用servlet成功前台没有调用success
- PHP中使用POST发送请求的常用方式
- AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式
- AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式
- 浅谈使用Fiddler工具发送post请求(带有json数据)以及get请求(Header方式传参)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- 使用HttpWebRequest发送自定义POST请求(以http方式推送消息)
- 使用java程序发送post请求给servlet
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- 使用httppost请求发送form表单的方法
- PHP 使用curl库来发送GET,POST请求,处理json格式数据