.Net HttpResponseMessage receiving BadGateway when take more than 1min - php

I have a windows service .Net client comunicating with a PHP web service. The basic idea, the .Net client send information to php to write in data base, this process take araund 1min, the problem is that, when it takes more, the HttpResponseMessage comes with BadGateway, the funny thing is php actually write all that has to write in the database even when it takes more than 1min. I try to set the TimeOut of HttpClient to 10min:
//code for the stuff making
Logger.WriteLog("Sending Stuff");
using (var client = new HttpClient())
{
client.Timeout = new TimeSpan(0,10,0);
httpResponseMessage = client.PostAsync(url, content).Result;
Logger.WriteLog(httpResponseMessage.StatusCode.ToString());
Logger.WriteLog(httpResponseMessage.ToString());
}
But still, it fail with more than 1min response, the php web service it suppose to have a timeout of 10min too. I log the full response and i got the bad Gateway:
2016-04-25 11:12:24,045 INFO - *Sending Stuff
2016-04-25 11:13:23,413 INFO - OK
2016-04-25 11:13:23,414 INFO - StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Pragma: no-cache
Connection: close
Cache-Control: no-store, must-revalidate, no-cache, post-check=0, pre-check=0
Date: Mon, 25 Apr 2016 14:12:37 GMT
Set-Cookie: PHPSESSID=lbhsue3pstc8smqihkoe0nouj1; path=/
Server: Apache/2.2.15
Server: (CentOS)
X-Powered-By: PHP/5.6.19
Content-Length: 52
Content-Type: application/json; charset=UTF8
Expires: Thu, 19 Nov 1981 08:52:00 GMT
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
2016-04-25 11:14:42,931 INFO - *Sending Stuff
2016-04-25 11:15:44,273 INFO - BadGateway
2016-04-25 11:15:44,274 INFO - StatusCode: 502, ReasonPhrase: 'Proxy Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Connection: close
Date: Mon, 25 Apr 2016 14:14:43 GMT
Content-Length: 499
Content-Type: text/html; charset=iso-8859-1
}
//------------------------------------------------------------------------------------------------------------------------------------------------
2016-04-25 11:25:28,471 INFO - *Sending Stuff
2016-04-25 11:26:27,495 INFO - OK
2016-04-25 11:26:27,497 INFO - StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Pragma: no-cache
Connection: close
Cache-Control: no-store, must-revalidate, no-cache, post-check=0, pre-check=0
Date: Mon, 25 Apr 2016 14:25:41 GMT
Set-Cookie: PHPSESSID=ja6f30mqlnhtdl8p44hddr43c3; path=/
Server: Apache/2.2.15
Server: (CentOS)
X-Powered-By: PHP/5.6.19
Content-Length: 52
Content-Type: application/json; charset=UTF8
Expires: Thu, 19 Nov 1981 08:52:00 GMT
}
My hope was that, timeout where the reason of the badgateway but it make not difference. Can someone tell me what are the possibilities of the badgateway, even when the process in php finish the task. Thanks in advance!
PD:Sorry for my english

A timeout on the .NET end shouldn't generate an HTTP status code. It should throw a separate timeout exception. If you see an HTTP status code, that has to come from the server side.
Are you sure the web server itself (on the PHP end) isn't configured with a timeout for scripts?

Related

Network Failure while download Using PHP Script

I am getting network failure message in chrome but working on other browser using php download script.
I have below headers set to download.
cache-control: must-revalidate, post-check=0, pre-check=0
content-disposition: attachment; filename="export.csv"
content-length: 388436
content-type: application/octet-stream
date: Thu, 19 Jul 2018 13:41:02 GMT
expires: Wed, 19 Jul 2017 13:40:59 GMT
last-modified: Thu, 19 Jul 2018 13:41:02 GMT
pragma: public
server: nginx
set-cookie: admin=p5t1vd4e4u3j9g6gto5h046ki0; path=/admin; domain=xyz.com; secure; HttpOnly
status: 200
x-powered-by: PHP/7.0.29
I am getting error in chrome version 67.3396.99

Wordpress Site not Connecting thru online Cron Job Service

I am running a wordpress site and using an online cron job service to connect.
When i connect thru the service im getting "HTTP/1.1 404 Not Found" error.
This is the cron job link im using:
http://www.moongot.com/wp-content/plugins/WPFanMachineBasic/auto.php
This is the output:
X-Powered-By: PHP/5.5.22 Pragma: no-cache Date: Sat, 14 Apr 2018
22:00:59 GMT Content-Length: 4 Server: Apache/2 Set-Cookie:
PHPSESSID=3ef1b912c68e0502d03caccb87e0858b; path=/ Expires: Wed, 11
Jan 1984 05:00:00 GMT Cache-Control: no-cache, must-revalidate,
max-age=0 X-Endurance-Cache-Level: 2 Content-Type: text/html;
charset=UTF-8
DONE
I get the same via cURL:
curl -I http://www.moongot.com/wp-content/plugins/WPFanMachineBasic/auto.php
HTTP/1.1 404 Not Found
Date: Sat, 14 Apr 2018 22:29:13 GMT
Content-Type: text/html
Content-Length: 863
Connection: keep-alive
Keep-Alive: timeout=30
Server: Apache/2
X-Powered-By: PHP/5.5.22
Set-Cookie: PHPSESSID=2df00eb1132bf607a708d00aee845f8a; path=/
Pragma: no-cache
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Last-Modified: Wed, 02 Aug 2017 19:17:35 GMT
Accept-Ranges: bytes
and in Chrome:
So you'll want to figure out why your auto.php file is spitting out a 404 header.

Php Soap Headers Not Getting Set

I can not get my php to set a value in the SOAP Header.
my code is
$requestParams = 'Parameters I am passing into the WS'
$client = new SoapClient('https://webservice.wsdl',array(
'login' => "user",
'password' => "password",
'trace' => 1,
'sessionID' => "93b23bb6e611db8548f2ee9485bc7d17"));
$client->submitRequest($requestParams);
process returned code and it does return what I expect
then
$client->release();
which should release this sessionID
The Web service I am calling uses a sessionID variable and if one is not passed it will create one for you but you can only have 5 unique sessions created before it locks you out for 5 min.
If you open a session you can reuse it as long as you free a session off and don't have more than five open.
My problem is that everything works but the web service does not see my sessionID variable because every time I make a call it issues me a new sessionID. So it created 5 new sessionID's and I have no way of freeing them off when completed and I have to wait till they timeout on the server.
Web Service Call #1 string(414) "HTTP/1.1 200 OK Date: Fri, 01 Aug
2014 20:56:32 GMT Server: Apache Set-Cookie:
PHPSESSID=ee6d8e3f9e6e1b9dac35e10f917a69c8; path=/ Expires: Thu, 19
Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache,
must-revalidate, post-check=0, pre-check=0 Pragma: no-cache
Set-Cookie: PHPSESSID=ee6d8e3f9e6e1b9dac35e10f917a69c8; path=/
Content-Length: 560 Connection: close Content-Type: text/xml;
charset=utf-8 "
Web Service Call #2 string(414) "HTTP/1.1 200 OK Date: Fri, 01 Aug
2014 20:57:18 GMT Server: Apache Set-Cookie:
PHPSESSID=46d99f3b92d095981119b7050686967c; path=/ Expires: Thu, 19
Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache,
must-revalidate, post-check=0, pre-check=0 Pragma: no-cache
Set-Cookie: PHPSESSID=46d99f3b92d095981119b7050686967c; path=/
Content-Length: 560 Connection: close Content-Type: text/xml;
charset=utf-8 "
And So on Till I get
A connection refused and I have to wait. If the session ID was passed in the SOAP HEADER from the code above the response would look like.
string(414) "HTTP/1.1 200 OK Date: Fri, 01 Aug 2014 20:58:18 GMT
Server: Apache Set-Cookie: PHPSESSID=93b23bb6e611db8548f2ee9485bc7d17;
path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store,
no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache
Set-Cookie: PHPSESSID=93b23bb6e611db8548f2ee9485bc7d17; path=/
Content-Length: 560 Connection: close Content-Type: text/xml;
charset=utf-8 "
You need to use the _cookies and the __setCookie methods on the SoapClient class to get and set the Session ID for use during SOAP calls.
Your code would look like this:
$requestParams = 'Parameters I am passing into the WS'
$sessionId = "93b23bb6e611db8548f2ee9485bc7d17";
$client = new SoapClient('https://webservice.wsdl',array(
'login' => "user",
'password' => "password",
'trace' => 1));
$client->__setCookie('PHPSESSID', $sessionId);
// ... your requests
If you are making the calls in separate stateless runs of the script, then you also would need to setup some method of persistence to make the Session ID persist between uses.

Setting content-type using nginx and PHPExcel

I'm developing a site using Zend Framework 2 where the client wanted to be able to export some data to an Excel file. For this I went with PHPExcel, and I got it working on my local computer, where I run Apache2.
<?php
//Create a writer for Excel
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
// Get the data from php://output
// https://stackoverflow.com/questions/16551375/phpexcel-in-zend2-controller
ob_flush();
ob_start();
$objWriter->save('php://output');
$excelOutput = ob_get_clean();
//Get a new response object
$response = new \Zend\Http\Response();
//Set headers for the response object
$response->getHeaders()
->addHeaderLine('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT')
->addHeaderLine('Cache-Control: no-store, no-cache, must-revalidate')
->addHeaderLine('Cache-Control: post-check=0, pre-check=0')
->addHeaderLine('Pragma: no-cache')
->addHeaderLine('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
->addHeaderLine('Content-Disposition: attachment;filename="report.xlsx"');
//Set the content for the response object
$response->setContent($excelOutput);
return $response;
?>
The problem is that when I uploaded the project to my production server, running Nginx with a pretty standard setup using PHP-FPM, the Excel file isn't sent using the correct headers. It seems like they are overwritten with "text/html" which makes the browser show some garbled characters instead of making the file available for download.
I followed this (PHPExcel in Zend2 Controller) question for getting the content into a variable.
I can't figure out why this is happening, are the headers set differently using PHP on Apache2 than running Nginx with PHP-FPM?
Update: Returned headers
Apache on local computer:
HTTP/1.1 200 OK
Date: Sun, 11 May 2014 11:24:09 GMT
Server: Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.19
X-Powered-By: PHP/5.4.19
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: post-check=0, pre-check=0
Pragma: no-cache
Last-Modified: Sun, 11 May 2014 11:24:09 GMT
Content-Disposition: attachment;filename="report.xlsx"
Content-Length: 6551
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Nginx headers:
HTTP/1.1 200 OK
Server: nginx/1.1.19
Date: Sun, 11 May 2014 11:14:18 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.3.10-1ubuntu3.11
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Encoding: gzip

HTTP Headers difference - load page incrementally

I have an HTML page that shows a progress bar as it steps through a process. It uses flush() to send the data to the browser. I'm trying to get this to work in a Zend process which I'm short circuiting by specifically sending a header, content, then ending the process with an exit command.
The HTML page displays correctly (progress bar steps through being done). The Zend/PHP page only shows the finished page (not the steps). I'm assuming this is a header problem since the method (flush()) is identical.
In Chrome, the header for the HTML page comes up as:
HTTP/1.1 200 OK
Date: Fri, 27 Jul 2012 14:38:07 GMT
Server: Apache/2.2.16 (Unix) mod_ssl/2.2.16 OpenSSL/0.9.8r DAV/2 PHP/5.3.2
X-Powered-By: PHP/5.3.2
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html
And the header for the Zend/PHP page comes up as:
HTTP/1.1 200 OK
Date: Fri, 27 Jul 2012 14:44:13 GMT
Server: Apache/2.2.16 (Unix) mod_ssl/2.2.16 OpenSSL/0.9.8r DAV/2 PHP/5.3.2
X-Powered-By: PHP/5.3.2
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-cache
Pragma: no-cache
Keep-Alive: timeout=5, max=98
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8
The only header information I'm specifying in the PHP is:
header('Content-Type: text/html; charset=utf-8');
I'm using this code from this page: http://w3shaman.com/article/php-progress-bar-script
Any help would be appreciated. Thanks.
Call ob_flush() before you call flush() as Zend could have output buffering activated.
Mathieu had the fix. Adding ob_flush() before flush() in the Zend/PHP page fixed the problem. I'm not sure if Zend is activating output buffering as suggested or not.

Categories