function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AkoAko 

httprequests timing out from specific SF org

Hello,
I have been using Apex to do an httprequest to a specific website. All it does is a form post to a page. I have found that about 10% of the time, it fails with a "System.CalloutException: Read timed out" error. I've been testing this in the developer console.
I also have access to a separate Salesforce org, and I have run the exact same tests from there, and it has never gotten the Read Timed Out error.

I even wrapped the request in a try statement, and it will re-attempt if it fails the first time, but the second attempt fails 100% of the time.

I am wondering why one Salesforce org would be experiencing the issue whereas another one doesn't. The one having issues is on na5.

The code I am testing with is as follows, but I have replaced the website URL as I don't really want to post it publically, but the page is very simple and typically returns instantly, even if you post garbage content.
 
    HttpRequest req = new HttpRequest(); 
	integer debug=0;
	Map<String,String> headers = new Map<String, String>{};
	headers.put('content-type','application/x-www-form-urlencoded');
	String method = 'POST';
	String body='cmd=test';
	req.setMethod(method);
	req.setTimeout(30000);
	if(method == 'POST') { 
		system.debug('content-type is >' +headers.get('content-type'));
		req.setHeader('content-type', headers.get('content-type') );

		req.setBody(body); 
		req.setCompressed(false); // otherwise we hit a limit of 32000
	}

	req.setEndpoint('http://WEBSITEURLHERE.COM/');

	if ( ! headers.isEmpty() ) { 
		for(string k:headers.keySet() ) { 
			system.debug('setheader ' + k + ' => ' +headers.get(k) ); 
			req.setHeader(k, headers.get(k) ); 
		}
	}

	Http http = new Http();
	HttpResponse res;

	String resultString;
	String resToString;
	String resBody;
	String resStatus;
	Integer resStatusCode=0;
     
	resultString='';
	try {
		res = http.send(req);
	} catch(System.CalloutException e) {
		system.debug('Error...will retry');
    		System.debug(e);
    		res = http.send(req);
	}
	resToString=res.toString();
	resBody=res.getBody();
	resStatusCode = res.getStatusCode();
	resStatus = res.getStatus();
	resultString = resBody;
	System.debug(resStatusCode) ;

Does anyone have any idea why a certain SF org might experience sporadic time outs?
Thanks for any help.

 
James LoghryJames Loghry
Hi Ako, servers can vary from time to time in their performance.  Any number of factors can slow a pod down, and it's always a good idea to check http://trust.salesforce.com/status when it comes to Salesforce's pods.

Typically some pods are slower than others, but it's difficult to work around that.  The best you can do is keep your services, interactions, integrations as light weight as possible, and implement the best error handling (and perhaps retry / queueing logic) you can.

Hope that helps.