C# HTTP处理程序中的HTTP Post到另一个IP
场景是这样的,我有一个基本的HTTPHandler,从一个源接收JSON,处理得很好。然后,它需要发送到另一个与原稿无关的目的地,但以我的生命而言,无法将其发送到帖子。 使用wireshark它似乎从未发布数据,我想知道这是否是因为它使用了相同的HTTPContext,所以我部署为一个新线程-仍然不起作用。。我已经发布了代码对不起,如果有点混乱,我只是想在整理之前让它工作。 谢谢你的期待,我已经使用了许多文章在这里之前,他们一直是难以置信的有用C# HTTP处理程序中的HTTP Post到另一个IP,c#,http,post,httphandler,C#,Http,Post,Httphandler,场景是这样的,我有一个基本的HTTPHandler,从一个源接收JSON,处理得很好。然后,它需要发送到另一个与原稿无关的目的地,但以我的生命而言,无法将其发送到帖子。 使用wireshark它似乎从未发布数据,我想知道这是否是因为它使用了相同的HTTPContext,所以我部署为一个新线程-仍然不起作用。。我已经发布了代码对不起,如果有点混乱,我只是想在整理之前让它工作。 谢谢你的期待,我已经使用了许多文章在这里之前,他们一直是难以置信的有用 public void ProcessRe
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 );
}
}
对于现在看到这一点的人来说,我解决了这个问题,在我找到搜索词后,谷歌上出现了不少搜索结果!它被称为中继,代码如下。。。我遇到的主要问题是代理!由于我支持一个公司代理,因此响应被定向到代理,但失败了
public void ProcessRequest ( HttpContext context )
{
string retval = string.Empty;
string jsonString = string.Empty;
try
{
System.Diagnostics.Debug.WriteLine( "Received Post From GitLab." );
context.Response.ContentType = "text/plain; charset=utf-8"; //"application/xml";
HttpRequest original = context.Request;
context.Request.InputStream.Position = 0;
using (var inputStream = new StreamReader( context.Request.InputStream ))
jsonString = inputStream.ReadToEnd();
byte[] originalstream = System.Text.Encoding.ASCII.GetBytes( jsonString );
System.Diagnostics.Debug.WriteLine( originalstream );
#region respond to gitlab
retval =
string.Format(
"JSON Received {0}",
DateTime.Now.ToUniversalTime() );
context.Response.Write( retval ); //respond to gitlab, tell it received ok. (no 500)
System.Diagnostics.Debug.WriteLine( "Responded to GitLab" );
#endregion
var jsonObject = JsonConvert.DeserializeObject<GitlabPost>( jsonString );
System.Diagnostics.Debug.WriteLine( "Stringified JSON" );
//now have json object, spit it out to axosoft.
var hashedJSON = string.Format( "{0}{1}", jsonString, _axosoftAPIKey );
System.Diagnostics.Debug.WriteLine( hashedJSON );
SHA256 hash = SHA256.Create();
Byte[] result = hash.ComputeHash( System.Text.Encoding.ASCII.GetBytes( hashedJSON ) );
hashedJSON = result.Aggregate( "", ( current, x ) => current + string.Format( "{0:x2}", x ) );
System.Diagnostics.Debug.WriteLine( "Hashed JSON from GitLab" );
//now create the request to axosoft.
HttpWebRequest newRequest = (HttpWebRequest)WebRequest.Create( new Uri( _axosoftUrl + hashedJSON ) );
System.Diagnostics.Debug.WriteLine( "Opened Request to Axosoft." );
//copy over headers.
newRequest.ContentType = original.ContentType;
newRequest.Method = original.HttpMethod;
newRequest.UserAgent = original.UserAgent;
newRequest.Proxy = null;
Stream reqStream = newRequest.GetRequestStream();
reqStream.Write( originalstream, 0, originalstream.Length );
reqStream.Close();
System.Diagnostics.Debug.WriteLine( "written data to stream" );
try
{
//send the post.
newRequest.GetResponse();
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
{
var resp = (HttpWebResponse)ex.Response;
if (resp.StatusCode == HttpStatusCode.Created)
{
//post successful, carry on about your business.
System.Diagnostics.Debug.WriteLine( "201 Created" );
}
else if (resp.StatusCode == HttpStatusCode.NotFound)
{
//404
System.Diagnostics.Debug.WriteLine( "404 Not Found" );
}
else if (resp.StatusCode == HttpStatusCode.OK)
{
//200
System.Diagnostics.Debug.WriteLine( "200 OK" );
}
else
{
//unknown!
System.Diagnostics.Debug.WriteLine( string.Format( "Unknown Status: {0}", resp.StatusCode ) );
}
}
else
{ // dont actually throw exception in production code, that would be stupid...
throw new Exception("unknown exception in GetResponse(): " + ex.Message);
}
}
System.Diagnostics.Debug.WriteLine( "Sent Post Data.." );
}
catch (Exception error)
{
string err = error.Message;
System.Diagnostics.Debug.WriteLine( "Exception: " + err );
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 );
}
}
}
public void ProcessRequest ( HttpContext context )
{
string retval = string.Empty;
string jsonString = string.Empty;
try
{
System.Diagnostics.Debug.WriteLine( "Received Post From GitLab." );
context.Response.ContentType = "text/plain; charset=utf-8"; //"application/xml";
HttpRequest original = context.Request;
context.Request.InputStream.Position = 0;
using (var inputStream = new StreamReader( context.Request.InputStream ))
jsonString = inputStream.ReadToEnd();
byte[] originalstream = System.Text.Encoding.ASCII.GetBytes( jsonString );
System.Diagnostics.Debug.WriteLine( originalstream );
#region respond to gitlab
retval =
string.Format(
"JSON Received {0}",
DateTime.Now.ToUniversalTime() );
context.Response.Write( retval ); //respond to gitlab, tell it received ok. (no 500)
System.Diagnostics.Debug.WriteLine( "Responded to GitLab" );
#endregion
var jsonObject = JsonConvert.DeserializeObject<GitlabPost>( jsonString );
System.Diagnostics.Debug.WriteLine( "Stringified JSON" );
//now have json object, spit it out to axosoft.
var hashedJSON = string.Format( "{0}{1}", jsonString, _axosoftAPIKey );
System.Diagnostics.Debug.WriteLine( hashedJSON );
SHA256 hash = SHA256.Create();
Byte[] result = hash.ComputeHash( System.Text.Encoding.ASCII.GetBytes( hashedJSON ) );
hashedJSON = result.Aggregate( "", ( current, x ) => current + string.Format( "{0:x2}", x ) );
System.Diagnostics.Debug.WriteLine( "Hashed JSON from GitLab" );
//now create the request to axosoft.
HttpWebRequest newRequest = (HttpWebRequest)WebRequest.Create( new Uri( _axosoftUrl + hashedJSON ) );
System.Diagnostics.Debug.WriteLine( "Opened Request to Axosoft." );
//copy over headers.
newRequest.ContentType = original.ContentType;
newRequest.Method = original.HttpMethod;
newRequest.UserAgent = original.UserAgent;
newRequest.Proxy = null;
Stream reqStream = newRequest.GetRequestStream();
reqStream.Write( originalstream, 0, originalstream.Length );
reqStream.Close();
System.Diagnostics.Debug.WriteLine( "written data to stream" );
try
{
//send the post.
newRequest.GetResponse();
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
{
var resp = (HttpWebResponse)ex.Response;
if (resp.StatusCode == HttpStatusCode.Created)
{
//post successful, carry on about your business.
System.Diagnostics.Debug.WriteLine( "201 Created" );
}
else if (resp.StatusCode == HttpStatusCode.NotFound)
{
//404
System.Diagnostics.Debug.WriteLine( "404 Not Found" );
}
else if (resp.StatusCode == HttpStatusCode.OK)
{
//200
System.Diagnostics.Debug.WriteLine( "200 OK" );
}
else
{
//unknown!
System.Diagnostics.Debug.WriteLine( string.Format( "Unknown Status: {0}", resp.StatusCode ) );
}
}
else
{ // dont actually throw exception in production code, that would be stupid...
throw new Exception("unknown exception in GetResponse(): " + ex.Message);
}
}
System.Diagnostics.Debug.WriteLine( "Sent Post Data.." );
}
catch (Exception error)
{
string err = error.Message;
System.Diagnostics.Debug.WriteLine( "Exception: " + err );
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 );
}
}
}