[英]How do I copy a HttpRequest to another web service?
I have two identical web services currently installed on the same PC for testing purposes. 我目前在同一台PC上安装了两个相同的Web服务用于测试目的。 A request that is received by the first service, is suposed go to the second one as well. 第一个服务接收的请求也会被转到第二个服务。 My intention was to do this using a HttpModule. 我的意图是使用HttpModule来做到这一点。 I handle the request during application.BeginRequest. 我在application.BeginRequest期间处理请求。
public void AsyncForwardRequest(HttpRequest request)
{
try
{
// Prepare web request...
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(WSaddress);
req.Credentials = CredentialCache.DefaultCredentials;
req.Headers.Add("SOAPAction", request.Headers["SOAPAction"]);
req.ContentType = request.Headers["Content-Type"];
req.Accept = request.Headers["Accept"];
req.Method = request.HttpMethod;
// Send the data.
long posStream = request.InputStream.Position;
long len = request.InputStream.Length;
byte[] buff = new byte[len];
request.InputStream.Seek(0, SeekOrigin.Begin);
if (len < int.MaxValue && request.InputStream.Read(buff, 0, (int)len) > 0)
{
using (Stream stm = req.GetRequestStream())
{
stm.Write(buff, 0, (int)len);
}
request.InputStream.Position = posStream;
DebugOutputStream(request.InputStream);
request.InputStream.Seek(0, SeekOrigin.Begin);
IAsyncResult result = (IAsyncResult)req.BeginGetResponse(new AsyncCallback(RespCallback), req);
}
}
catch (Exception ex)
{
App.Error2(String.Format("RequestDuplicatorModule - BeginRequest; ERROR begin request: {0}", ex.Message), ex);
}
private static void RespCallback(IAsyncResult asynchronousResult)
{
try
{
// State of request is asynchronous.
var req = (HttpWebRequest)asynchronousResult.AsyncState;
WebResponse resp = (HttpWebResponse)req.EndGetResponse(asynchronousResult);
Stream responseStream = resp.GetResponseStream();
System.IO.Stream stream = responseStream;
Byte[] arr = new Byte[1024 * 100];
stream.Read(arr, 0, 1024 * 100);
if (arr.Length > 0)
{
string body = (new ASCIIEncoding()).GetString(arr);
Debug.Print(body);
}
}
catch (WebException ex)
{
App.Error2(String.Format("RequestDuplicatorModule - BeginRequest; ERROR begin request: {0}", ex.Message), ex);
}
}
This (HttpWebResponse)req.EndGetResponse(asynchronousResult) throws an exception: 500 Internal Server Error and fails. 此(HttpWebResponse)req.EndGetResponse(asynchronousResult)抛出异常:500内部服务器错误并失败。 The original request goes through fine. 原始请求很好。
request.InnerStream: ... request.InnerStream:...
Does this have anything to do with the web service addresses being different? 这与Web服务地址有什么不同吗? In my case they're: http://localhost/WS/service.asmx http://localhost/WS_replicate/service.asmx 在我的例子中,他们是: http://localhost/WS/service.asmx http://localhost/WS_replicate/service.asmx
I would at least implement this, to be able to read the content of a httpwebrequest that has a status 500: 我至少会实现这一点,以便能够读取状态为500的httpwebrequest的内容:
catch (WebException ex)
{
HttpWebResponse webResponse = (HttpWebResponse)ex.Response;
Stream dataStream = webResponse.GetResponseStream();
if (dataStream != null)
{
StreamReader reader = new StreamReader(dataStream);
response = reader.ReadToEnd();
}
}
Not answering your question though, i would suggest having a simple amsx that reads the request, and posts the request to the two different web services. 虽然没有回答你的问题,我建议有一个简单的amsx来读取请求,并将请求发布到两个不同的Web服务。 If you need some code for that let me know. 如果您需要一些代码,请告诉我。
Additional advantages would be that the asmx will be easier to support for the developer as well as the one dealing with the deployment. 其他优点是asmx将更容易支持开发人员以及处理部署的人员。 You could add configuration options to make the actual url's dynamic. 您可以添加配置选项以使实际URL动态化。
Was able to modify the AsyncForwardRequest method so that I can actually edit the SoapEnvelope itself: 能够修改AsyncForwardRequest方法,以便我可以实际编辑SoapEnvelope本身:
string buffString = System.Text.UTF8Encoding.UTF8.GetString(buff);
using (Stream stm = req.GetRequestStream())
{
bool stmIsReadable = stm.CanRead; //false, stream is not readable, how to get
//around this?
//solution: read Byte Array into a String,
//modify <wsa:to>, write String back into a
//Buffer, write Buffer to Stream
buffString = buffString.Replace("http://localhost/WS/Service.asmx", WSaddress);
//write modded string to buff
buff = System.Text.UTF8Encoding.UTF8.GetBytes(buffString);
stm.Write(buff, 0, (int)buff.Length);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.