Windows store apps: web request delayed with 1 minute - c#

My app needs to perform a lot of diffrerent requests (~ 1-2 per second). I have a type of requests which runs continuously(RequestMade -> ResponseReceived -> RequestMade ->...).
If I enter another screen I should start a new set of requests to run continuously as long as I remain in that screen.
The problem is the that the new set of requests (the initial request) is delayed with ~ 1 minute.
Below is posted the code used to perform the requests.
Please note this line is printed out to console at, let's say 12:00:
Debug.WriteLine("Writing RequestStream ("+_request.GetType().FullName + " | " + DateTime.Now.ToLocalTime()+")");
And the lines:
Debug.WriteLine("Request is posting.....(" + _request.GetType().FullName + " | " + DateTime.Now.ToLocalTime()+")");
Debug.WriteLine("Reading ResponseStream (" + _request.GetType().FullName + " | " + DateTime.Now.ToLocalTime() + ")");
are printed at 12:01 (after 1 minute....)
private class RequestResponseTask
{
private Uri _uri = null;
private string _uriAddress = null;
private WebRequest _webRequest = null;
private ARequest _request = null;
private JsonTextParser _parser = null;
private RequestState _requestState = null;
public RequestResponseTask(ARequest request)
{
// uri address
_uriAddress = CVSCustomRelease.Instance.ReleaseSettings.SelectedPrivateLabel.GetServer(LoginSettings.Instance.SelectedServer).Address
+ CONTEXTUAL_REQUEST_PATH;
// uri
_uri = new Uri(_uriAddress);
// request
_request = request;
_parser = new JsonTextParser();
_requestState = new RequestState(_request);
}
public void StartRequest()
{
Debug.WriteLine("Starting RUN.......(" + _request.GetType().FullName + " | " + DateTime.Now.ToLocalTime() + ")");
Task.Factory.StartNew(() =>
{
Debug.WriteLine("RUN Started. - for "+_request.GetType().FullName + " | " + DateTime.Now.ToLocalTime());
_request.ResponseReceived = false;
Debug.WriteLine("Before WebRequest ("+_request.GetType().FullName + " | " + DateTime.Now.ToLocalTime()+")");
_webRequest = WebRequest.Create(_uri);
Debug.WriteLine("after WebRequest (" + _request.GetType().FullName + " | " + DateTime.Now.ToLocalTime() + ")");
_webRequest.ContentType = "text/x-gwt-rpc;charset=utf-8";
_webRequest.Method = "Post";
_requestState.Request = _webRequest;
// Start the Asynchronous 'BeginGetRequestStream' method call.
Debug.WriteLine("Writing RequestStream ("+_request.GetType().FullName + " | " + DateTime.Now.ToLocalTime()+")");
IAsyncResult r = (IAsyncResult)_webRequest.BeginGetRequestStream(
new AsyncCallback(PostRequest), _requestState);
_requestState.ResetEvent.WaitOne();
Debug.WriteLine("Reading ResponseStream (" + _request.GetType().FullName + " | " + DateTime.Now.ToLocalTime() + ")");
IAsyncResult asyncResp = (IAsyncResult)_webRequest.BeginGetResponse(
new AsyncCallback(ReadResponse), _requestState);
});
}
private void PostRequest(IAsyncResult asynchronousResult)
{
Debug.WriteLine("======================================================");
Debug.WriteLine("Request is posting.....(" + _request.GetType().FullName + " | " + DateTime.Now.ToLocalTime()+")");
// End the Asynchronus Request.
Stream streamResponse = _webRequest.EndGetRequestStream(asynchronousResult);
ARequest request = _requestState.OriginalRequest;
request.UpdateTimestampRealtime();
string postData = request.GetPostData();
EventsLog.Instance.WriteEvent("Request: " + postData);
// Create a string that is to be posted to the uri.
// Convert the string into a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
EventsLog.Instance.CurrentSession.AddTraficAmount(TraficType.Outgoing, byteArray.Length);
// Write the data to the stream.
streamResponse.Write(byteArray, 0, postData.Length);
streamResponse.Flush();
Debug.WriteLine("Request POSTED.");
Debug.WriteLine("======================================================");
_requestState.ResetEvent.Set();
}
private async void ReadResponse(IAsyncResult asyncResult)
{
_requestState = (RequestState)asyncResult.AsyncState;
WebRequest myWebRequest = _requestState.Request;
WebResponse response = (WebResponse)myWebRequest.EndGetResponse(asyncResult);
Stream responseStream = response.GetResponseStream();
StreamReader streamRead = new StreamReader(responseStream);
string responseString = await streamRead.ReadToEndAsync();
byte[] byteArray = Encoding.UTF8.GetBytes(responseString);
EventsLog.Instance.CurrentSession.AddTraficAmount(TraficType.Incomming, byteArray.Length);
// build response object
JsonObject jsonObject = _parser.Parse(responseString);
EventsLog.Instance.WriteEvent("Response: " + jsonObject.ToString() + "\nFor Request: " + _requestState.OriginalRequest.RequestId + " | " +_requestState.OriginalRequest.GetType().FullName);
_requestState.ResetEvent.Reset();
// notify listeners
_requestState.OriginalRequest.ResponseReceived = true;
_requestState.OriginalRequest.NotifyResponseListeners(jsonObject as JsonObjectCollection);
}
}
performing a request:
new RequestResponseTask(_request).StartRequest();

My guess: Your code blocks, which leads to a timeout on another thread (that's where the 1 minute comes from) which then leads to unblocking the first thread.
You have things like BeginGetRequestStream and ResetEvent.WaitOne in your code - which shouldn't be needed in a Windows Store code like yours. Try to use WebClient's async methods or HttpClient instead. Use await instead of WaitOne.

Related

Getting FullContent from blobBaseClient.DownloadToAsync

I am trying to download blob using BlockBlobClient DownloadToAsync method.
As mentioned in documentation, I provided required param, but getting only partial content.
Related code is:
string messageJson = string.Empty;
try
{
var blobBaseClient = new BlobBaseClient(_connectionString, _containerName, blobName);
Stream stream = new MemoryStream();
CancellationToken cancelToken = new CancellationToken();
StorageTransferOptions storageTransferOptions = new StorageTransferOptions
{
//bytes * 1000000 = MB
InitialTransferSize = blobInitialChunkSize.Value * 1000000,
MaximumConcurrency = blobChunkUploadMaxThreadCount.Value,
MaximumTransferSize = long.MaxValue
};
BlobRequestConditions blobRequestConditions = new BlobRequestConditions();
Response response = await blobBaseClient.DownloadToAsync(stream, blobRequestConditions, storageTransferOptions, cancelToken);
if(response.Status == Convert.ToInt32(HttpStatusCode.OK)
{
using (StreamReader streamReader = new StreamReader(stream))
{
messageJson = streamReader.ReadToEnd();
}
}
else
{
_logger.LogInformation("response.Headers.ContentType : " + response.Headers.ContentType + DateTime.Now.ToString());
_logger.LogInformation("response.Headers.ContentLength : " + response.Headers.ContentLength + DateTime.Now.ToString());
_logger.LogInformation("response.Headers.ETag : " + response.Headers.ETag + DateTime.Now.ToString());
_logger.LogInformation("response.Headers.RequestId : " + response.Headers.RequestId + DateTime.Now.ToString());
_logger.LogInformation("response.Status : " + response.Status + DateTime.Now.ToString());
_logger.LogInformation("response.ReasonPhrase : " + response.ReasonPhrase + DateTime.Now.ToString());
_logger.LogInformation("stream : " + stream + DateTime.Now.ToString());
}
catch (Exception ex)
{
_logger.LogInformation(ex.Message + " " + DateTime.Now.ToString());
_logger.LogInformation(ex.StackTrace + " " + DateTime.Now.ToString());
_logger.LogInformation(ex.Source + " " + DateTime.Now.ToString());
_logger.LogInformation(ex.InnerException + " " + DateTime.Now.ToString());
throw;
}
return messageJson;
the result I got is else block(not exception) & log values :
response.Headers.ContentType : application / octet - stream
response.Headers.ContentLength : 189778220
response.Headers.ETag : 0x8D860678531E07B
response.Status : 206
response.ReasonPhrase : Partial Content
stream: System.IO.MemoryStream
messageJson:
I want to know how can I get full data instead of Partial Data ? Any pointers/Help will be appreciated.
Try the code bellow with DownloadAsync method, it will return 200.
BlobServiceClient blobServiceClient = new BlobServiceClient(_connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(_containerName);
BlobClient blobClient = containerClient.GetBlobClient(blobName);
if (await blobClient.ExistsAsync())
{
var response = await blobClient.DownloadAsync();
using (var streamReader = new StreamReader(response.Value.Content))
{
while (!streamReader.EndOfStream)
{
var line = await streamReader.ReadLineAsync();
Console.WriteLine(line);
}
}
Console.WriteLine(response);
}
Azure Blob storage supports return 206 response if you're using API version 2011-01-18 or later. As the documentation says:
A successful operation to read the full blob returns status code 200
(OK).
A successful operation to read a specified range returns status code
206 (Partial Content).
As suggested on Bug I asked on Github, I have added success condition for HTTP 206 too and it is working fine:
if ((response.Status == Convert.ToInt32(HttpStatusCode.OK))
|| response.Status == Convert.ToInt32(HttpStatusCode.PartialContent))
{
using StreamReader streamReader = new StreamReader(response.ContentStream);
stream.Position = 0;
messageJson = streamReader.ReadToEnd();
}

Unauthorized Access Exception While Using Yahoo Weather API

I have done code to access yahoo weather API with oath by following all steps provided by yahoo in documentation as
1) Create Yahoo account
2) Create App
3) White list App
4) C# code to access yahoo weather API with oath
I am getting Unauthorized access exception while requesting API.
Here is the code :
public class WeatherYdn
{
public static void Main(string[] args)
{
const string appId = "YOUR-WHITELISTED-APPID";
const string consumerKey = "YOUR-CONSUMER-KEY";
const string consumerSecret = "YOUR-SECRET-KEY";
const string url = "https://weather-ydn-yql.media.yahoo.com/forecastrss";
string timestamp = StringHelper.GenerateTimeStamp();
String oauthNonce = StringHelper.GenerateNonce();
IList<string> parameters = new List<string>();
parameters.Add("oauth_consumer_key=" + consumerKey);
parameters.Add("oauth_nonce=" + oauthNonce);
parameters.Add("oauth_signature_method=HMAC-SHA1");
parameters.Add("oauth_timestamp=" + timestamp);
parameters.Add("oauth_version=1.0");
// Make sure value is encoded
parameters.Add("location=" + HttpUtility.UrlEncode("pune,in", Encoding.UTF8));
parameters.Add("format=json");
((List<string>) parameters).Sort();
StringBuilder parametersList = new StringBuilder();
for (int i = 0; i < parameters.Count; i++)
{
parametersList.Append(((i > 0) ? "&" : "") + parameters.ElementAt(i));
}
var signatureString = "GET&" +
HttpUtility.UrlEncode(url,Encoding.UTF8) + "&" +
HttpUtility.UrlEncode(parametersList.ToString(), Encoding.UTF8);
string signature = null;
try
{
string secretAccessKey = consumerSecret;
byte[] secretKey = Encoding.UTF8.GetBytes(secretAccessKey);
HMACSHA1 hmac = new HMACSHA1(secretKey);
hmac.Initialize();
byte[] bytes = Encoding.UTF8.GetBytes(signatureString);
byte[] rawHmac = hmac.ComputeHash(bytes);
signature = Convert.ToBase64String(rawHmac);
}
catch (Exception e)
{
Console.WriteLine("Unable to append signature");
}
string authorizationLine = "OAuth " +
"oauth_consumer_key=\"" + consumerKey + "\", " +
"oauth_nonce=\"" + oauthNonce + "\", " +
"oauth_timestamp=\"" + timestamp + "\", " +
"oauth_signature_method=\"HMAC-SHA1\", " +
"oauth_signature=\"" + signature + "\", " +
"oauth_version=\"1.0\"";
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url + "?location=pune,in&format=json");
request.Headers.Add("Authorization", authorizationLine);
request.Headers.Add("Yahoo-App-Id", appId);
request.ContentType = "application/json; charset=UTF-8";
request.Accept = "application/json";
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
Console.WriteLine(readStream.ReadLine());
}
}
In which line you get the error? The GetResponse() returns it?
I think that the credentials you are using (appId,consumerKey,consumerSecret) are invalid!
public string appId = "Your app-id";
public string consumerKey = "Your-consumer key";
public string consumerSecret = "Your Consumer Secret key";
// GET: api/Random
[HttpGet("{CityName}")]
public async Task<IActionResult> GetAsync([FromUri] string CityName)
{
string urlss = "https://weather-ydn-yql.media.yahoo.com/forecastrss?location=";
string url = urlss + CityName+ "&format=json&u=c";
JObject jresult;
using (var client = new HttpClient())
{
try
{
var webClient = new WebClient();
webClient.Headers.Add(AssembleOAuthHeader());
var d = webClient.DownloadString(url);
jresult = JObject.Parse(d);
var json_jsonstring = Newtonsoft.Json.JsonConvert.SerializeObject(jresult);
return Ok(json_jsonstring);
}
catch (HttpRequestException httpRequestException)
{
return BadRequest($"Error getting weather from Yahoo Weather: {httpRequestException.Message}");
}
}
}
public string AssembleOAuthHeader()
{
return "Authorization: OAuth " +
"realm=\"yahooapis.com\"," +
"oauth_consumer_key=\"" + consumerKey + "\"," +
"oauth_nonce=\"" + Guid.NewGuid() + "\"," +
"oauth_signature_method=\"PLAINTEXT\"," +
"oauth_timestamp=\"" + ((DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1).Ticks) / (1000 * 10000)) +
"\"," +
"oauth_version=\"1.0\"," +
"oauth_signature=\"" + consumerSecret + "%26\"," +
"oauth_callback=\"oob\"";
}
For yahoo weather new authentication you can use this python library
yahoo-weather
I think your code is fine. The problem lies with a poorly implemented URL Decode on Yahoo's end. Java URL Encode encodes with uppercase while .net HTTPUtility.URLEncode encodes in lower case. I created an extension method for a string that will rectify the problem and URL Encode in a way that the Yahoo API can handle. After doing that all worked well (I had the exact same problem you did).
<Extension>
Public Function UppercaseURLEncode(ByVal sourceString As String) As String
Dim temp As Char() = HttpUtility.UrlEncode(sourceString).ToCharArray()
For i As Integer = 0 To temp.Length - 2
If temp(i).ToString().Equals("%", StringComparison.OrdinalIgnoreCase) Then
temp(i + 1) = Char.ToUpper(temp(i + 1))
temp(i + 2) = Char.ToUpper(temp(i + 2))
End If
Next
Return New String(temp)
End Function
//Here Is The Working Code :
public class YWSample
{
const string cURL = "https://weather-ydn-yql.media.yahoo.com/forecastrss";
const string cAppID = "Your-App-ID";
const string cConsumerKey = "Your-Consumer-Key";
const string cConsumerSecret = "Your-Consumer-Secret";
const string cOAuthVersion = "1.0";
const string cOAuthSignMethod = "HMAC-SHA1";
const string cWeatherID = "woeid=727232"; // Amsterdam, The Netherlands
const string cUnitID = "u=c"; // Metric units
const string cFormat = "xml";
//Code to get timestamp
static string _get_timestamp()
{
var lTS = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
return Convert.ToInt64(lTS.TotalSeconds).ToString();
}
//Code to get nonce
static string _get_nonce()
{
return Convert.ToBase64String(
new ASCIIEncoding().GetBytes(
DateTime.Now.Ticks.ToString()
)
);
} // end _get_nonce
static string _get_auth()
{
var lNonce = _get_nonce();
var lTimes = _get_timestamp();
var lCKey = string.Concat(cConsumerSecret, "&");
var lSign = $"format={cFormat}&" + $"oauth_consumer_key={cConsumerKey}&" + $"oauth_nonce={lNonce}&" +
$"oauth_signature_method={cOAuthSignMethod}&" + $"oauth_timestamp={lTimes}&" +
$"oauth_version={cOAuthVersion}&" + $"{cUnitID}&{cWeatherID}";
lSign = string.Concat(
"GET&", Uri.EscapeDataString(cURL), "&", Uri.EscapeDataString(lSign)
);
using (var lHasher = new HMACSHA1(Encoding.ASCII.GetBytes(lCKey)))
{
lSign = Convert.ToBase64String(
lHasher.ComputeHash(Encoding.ASCII.GetBytes(lSign))
);
} // end using
return "OAuth " +
"oauth_consumer_key=\"" + cConsumerKey + "\", " +
"oauth_nonce=\"" + lNonce + "\", " +
"oauth_timestamp=\"" + lTimes + "\", " +
"oauth_signature_method=\"" + cOAuthSignMethod + "\", " +
"oauth_signature=\"" + lSign + "\", " +
"oauth_version=\"" + cOAuthVersion + "\"";
} // end _get_auth
public static void Main(string[] args)
{
const string lURL = cURL + "?" + cWeatherID + "&" + cUnitID + "&format=" + cFormat;
var lClt = new WebClient();
lClt.Headers.Set("Content-Type", "application/" + cFormat);
lClt.Headers.Add("Yahoo-App-Id", cAppID);
lClt.Headers.Add("Authorization", _get_auth());
Console.WriteLine("Downloading Yahoo weather report . . .");
var lDataBuffer = lClt.DownloadData(lURL);
var lOut = Encoding.ASCII.GetString(lDataBuffer);
Console.WriteLine(lOut);
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}//end of Main
} // end YWSample

Facebook active token failure C# HttpWebRequest

I obtain an access_token OK from Facebook, but whenever I try to use it, it fails (bad request).
It looks like the access_token is not being sent to the server correctly. I have used Server.UrlEncode to encode it.
Any ideas what I am doing wrong?
string ourAccessToken = "unknown";
//--------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
getAccessToken();
getMe();
}
// -----------------------
private void getAccessToken()
{
string result = "unknown";
try
{
// get app access token
string thisURL = "https://graph.facebook.com/oauth/access_token";
thisURL += "?client_id=" + ourClientID;
thisURL += "&client_secret=" + ourClientSecret;
thisURL += "&grant_type=client_credentials";
thisURL += "&redirect_uri=" + Server.UrlEncode(ourSiteRedirectURL);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create( thisURL);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
HttpStatusCode rc = response.StatusCode;
if( rc == HttpStatusCode.OK)
{
StreamReader Sreader = new StreamReader( response.GetResponseStream());
result = Sreader.ReadToEnd();
Sreader.Close();
}
response.Close();
}
catch (Exception exc)
{
result = "ERROR : " + exc.ToString();
}
Response.Write( "<br>result=[" + result + "]");
// extract accessToken
string accessToken = "";
int equalsAt = result.IndexOf( "=");
if( equalsAt >= 0 && result.Length > equalsAt) accessToken = (result.Substring( equalsAt + 1)).Trim();
Response.Write( "<br>accessToken=[" + accessToken + "]");
ourAccessToken = accessToken;
}
// -----------------------
private void getMe()
{
string result = "unknown";
try
{
string thisURL = "https://graph.facebook.com/me?access_token=" + Server.UrlEncode(ourAccessToken);
Response.Write("<br>thisURL=" + thisURL);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create( thisURL);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
HttpStatusCode rc = response.StatusCode;
if( rc == HttpStatusCode.OK)
{
StreamReader Sreader = new StreamReader( response.GetResponseStream());
result = Sreader.ReadToEnd();
Sreader.Close();
}
response.Close();
}
catch (Exception ex)
{
Response.Write("<br>getMe Exc: " + ex.Message.ToString() + "<br>");
}
Response.Write("<br>getMe result = " + result + "<br><br>");
}
Thanks
Right settings in App-Dashboard? If you active "Native/Desktop" you can not send API-Calls with this method, see:
https://developers.facebook.com/docs/facebook-login/access-tokens?locale=en_US#apptokens
After a lot of trial and error, I conclude that an App Access Token is not relevant, and that the ClientID and ClientSecret should be used directly. I want my App to generate a set of photographs of registered users. Because the server is making the call, there is no meaning to "me". A set of data can be obtained by preparing a batch process:
string p1 = "access_token=" + Server.UrlEncode(ourClientID + "|" + ourClientSecret);
string p2 = "&batch=" +
Server.UrlEncode( " [ { \"method\": \"get\", \"relative_url\": \"" + chrisFBID + "?fields=name,picture.type(square)\" }, " +
" { \"method\": \"get\", \"relative_url\": \"" + johnFBID + "?fields=name,picture.type(large)\" }, " +
" { \"method\": \"get\", \"relative_url\": \"" + stephFBID + "?fields=name,picture.type(large)\" } ]");
string responseData = "";
try
{
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("https://graph.facebook.com/");
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
byte[] bytedata = Encoding.UTF8.GetBytes(p1 + p2);
httpRequest.ContentLength = bytedata.Length;
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytedata, 0, bytedata.Length);
requestStream.Close();
HttpWebResponse httpWebResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
responseData = reader.ReadToEnd();
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString() + "<br>");
}
Response.Write("<br>" + responseData + "<br><br>");
I also conclude that the FB documentation suffers from the usual fatal flaw of documentation: it has been written by an expert and never tested on a novice user before release.

Middleware - performance issues

I'm developing a Middleware for my client and it's found that there are some delay when executing the process.
We are currently investigating what's the exact causes of the issue, from architecture design to coding.
Below are part of the scripts we are calling, and just wondering if the codes itself is optimized and properly written?
There's no error from the codes below, just wondering if there's any performance or coding issues. Tks
public string getResponse(System.Type type, Object input, string taskName, string method)
{
string response = string.Empty;
try
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(type);
MemoryStream mem = new MemoryStream();
ser.WriteObject(mem, input);
string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
using (WebClient webClient = new WebClient())
{
webClient.Headers["Content-type"] = "application/json";
webClient.Encoding = Encoding.UTF8;
logger.Info("Sending [" + method + "] request to API[" + string.Format(Configuration.getTargetURI(), taskName) + "]");
response = webClient.UploadString(string.Format(Configuration.getTargetURI(), taskName), method, data);
logger.Info("Response of API[" + string.Format(Configuration.getTargetURI(), taskName) + "] successfully retrieved");
logger.Debug("Response: " + response);
}
}
catch (Exception ex)
{
logger.Info("Failed to invoke [" + method + "] request to API[" + string.Format(Configuration.getTargetURI(), taskName) + "], Error: " + ex.Message);
logger.Error(ex.Message + ex.StackTrace);
throw ex;
}
return response;
}
public string getResponseNoInput(string taskName, string method)
{
string response = string.Empty;
try
{
WebRequest req = WebRequest.Create(String.Format(Configuration.getTargetURI(), taskName));
req.Method = method;
req.ContentType = "application/json; charset=utf-8";
logger.Info("Sending [" + method + "] request to API[" + string.Format(Configuration.getTargetURI(), taskName) + "]");
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
response = sr.ReadToEnd();
}
logger.Info("Response of API[" + string.Format(Configuration.getTargetURI(), taskName) + "] successfully retrieved");
logger.Debug("Response: " + response);
}
catch(Exception ex)
{
logger.Info("Failed to invoke [" + method + "] request to API[" + string.Format(Configuration.getTargetURI(), taskName) + "], Error: " + ex.Message);
logger.Error(ex.Message + ex.StackTrace);
throw ex;
}
return response;
}

Authorize.NET test account working fine on local host but not on live site

i m testing authorize.net payments and it is working fine on local host payment processing is all good. but when i upload it to my live site with test accounts i get error with
**Object reference not set to an instance of an object. at Billing.readHtmlPage(String url)**
string[] authorizeServer = readHtmlPage("https://test.authorize.net/gateway/transact.dll").Split('|');
//Error is Here
if (authorizeServer[0].ToLower() == "approved" || authorizeServer[0].ToLower() == "1")
{
//Process Payment
}
private String readHtmlPage(string url)
{
String result = "";
//Test Account ID
String strPost =
"x_login=xxxxx"&x_type=AUTH_CAPTURE&x_method=CC&x_tran_key=xxxxx&x_relay_response=&FALSE&" + "x_card_num=" + ccNum.Text + "&x_exp_date=" + ddl_CCM.SelectedValue + "/" +
ddl_CCY.SelectedValue +
"&x_amount=" + lbl_Gtotal.Text +
"&x_first_name=" + ccFName.Text + "&x_last_name=" + ccLName.Text +
"&x_address=" + Server.UrlEncode(hf_street.Value) + "&x_city=" +
hf_city.Value +
"&x_state=" + hf_state.Value + "&x_zip=" + hf_zip.Value;
StreamWriter myWriter = null;
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
objRequest.Method = "POST";
objRequest.ContentLength = strPost.Length;
objRequest.ContentType = "application/x-www-form-urlencoded";
try
{
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(strPost);
}
catch (Exception e)
{
return e.Message;
}
finally
{
myWriter.Close();
}
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream()))
{
result = sr.ReadToEnd();
sr.Close();
}
return result;
}
Any Help would be nice and much helpful thanks

Categories

Resources