Scenario is this, i have a basic HTTPHandler, receives JSON from one source, processes this fine. It then needs to send to another destination (unrelated to the original), but for the life of me, cannot get it to post. Using wireshark it seems to never post the data, i wondered if it was because it was using the same HTTPContext so i deployed as a new thread - still doesnt work.. I have posted the code (sorry if a bit messy, im just trying to get it to work before tidying). Thank you in anticipation, i have used many of the articles on here before and they have always been incredibly useful!
public void ProcessRequest ( HttpContext context )
{
string retval = string.Empty;
string jsonString = string.Empty;
try
{
context.Response.ContentType = "text/plain; charset=utf-8";
context.Request.InputStream.Position = 0;
using ( var inputStream = new StreamReader( context.Request.InputStream ) )
jsonString = inputStream.ReadToEnd();
//debug only
var jsonObject = JsonConvert.DeserializeObject<GitlabPost>( jsonString );
retval =
string.Format(
"JSON Received {0}",
DateTime.Now.ToUniversalTime() );
context.Response.Write( retval ); //respond to gitlab, tell it received ok.
//now have json object, spit it out to axosoft.
var hashedJSON = string.Format("{0}{1}", jsonString, AXOSOFT_API_KEY);
SHA256 hash = SHA256Managed.Create();
Byte[] result = hash.ComputeHash( System.Text.Encoding.ASCII.GetBytes( hashedJSON ) );
hashedJSON = result.Aggregate( "", ( current, x ) => current + string.Format( "{0:x2}", x ) );
Thread jsonPostThread = new Thread(() => SendAxosoftJSON(jsonString, hashedJSON) ) ;
jsonPostThread.Start();
}
catch (Exception error)
{
string err = error.Message;
context.Response.ContentType = "text/plain; charset=utf-8"; //"application/xml";
retval =
string.Format(
"JSON Post Failed at {0} because {1}",
DateTime.Now.ToUniversalTime(), err );
context.Response.Write( retval );
}
}
The Thread making the post is this:
private void SendAxosoftJSON(string jsonString, string hashedJSON)
{
var req =
(HttpWebRequest)
WebRequest.Create( AXOSOFT_URL + hashedJSON );
req.ContentType = "application/json";
req.Method = "POST";
using (var sw = new StreamWriter( req.GetRequestStream() ))
{
sw.Write( jsonString );
sw.Flush();
sw.Close();
}
req.GetResponse();
}
For anyone now looking at this, i solved the problem, there were quite a few results on google once i had figured the search terms out! Its called relaying, the code is below... the main problem i had was the proxy! as i am behind a corporate proxy, the response was being directed to the proxy, and failing.