HTTP 发布到 HTTP 处理程序中的另一个 IP
本文关键字:HTTP 另一个 IP 处理 程序 | 更新日期: 2023-09-27 18:30:41
场景是这样的,我有一个基本的HTTPHandler,从一个来源接收JSON,处理这个很好。然后它需要发送到另一个目的地(与原始目的地无关),但对于我的生活,无法让它发布。使用 wireshark 它似乎从不发布数据,我想知道是否是因为它使用相同的 HTTPContext,所以我部署为新线程 - 仍然不起作用。我已经发布了代码(对不起,如果有点混乱,我只是想在整理之前让它工作)。谢谢你的期待,我以前在这里使用过很多文章,它们一直非常有用!
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 );
}
}
发布帖子的线程是这样的:
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();
}
对于现在看到这个的人来说,我解决了这个问题,一旦我弄清楚了搜索词,谷歌上就会有很多结果!它叫中继,代码如下...我遇到的主要问题是代理!由于我在公司代理后面,响应被定向到代理,并且失败了。
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 );
}
}
}