Here we talk about service proxy. The application scenario is as follows:
Site A has a JS script that needs to call the service interface of Site B. However, Site A and site B are not under the same domain name, which is related to cross-origin access, one solution is to use a proxy to take over all calls to Site B services.
Server: acrossserver. ashx Public Class Acrossserver: ihttphandler {
Public Void Processrequest (httpcontext context ){
Context. response. contenttype = " Text/plain " ;
string clientkey = context. request [ " clientkey " ];
string clientip = context. request [ " clientip " ];
Context. response. Write (String. Format ("Acrossserver: clientkey = {0}, clientip = {1}", Clientkey, clientip ));
}
}
proxy service:
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> Public class javasssclient: ihttphandler {
Public void processrequest (httpcontext context) {
context. response. contenttype = " text/plain " ;
// Construct Post Data
String Clientkey = " Af8b908b-7735-4db6-9d8f-eef05b2eef69 " ;
String Clientip = Context. Request. userhostaddress;
Stringbuilder sb = New Stringbuilder ();
SB. appendformat ( " Clientkey = {0} & clientip = {1} " , Clientkey, clientip );
Foreach ( String Key In Context. Request. Form. allkeys)
{
SB. appendformat ( " & {0 }={ 1} " , Key, context. Request. Form [Key]);
}
String Postdata = SB. tostring ();
Byte [] Data = Encoding. utf8.getbytes (postdata );
// Httpwebrequest
String URL = String . Format ( " Http://my2.csdn.net/Samples/CJB3/AcrossServer.ashx " );
Httpwebrequest request = (Httpwebrequest) httpwebrequest. Create (URL );
Request. Method = " Post " ;
Request. contenttype = " Application/X-WWW-form-urlencoded " ;
Request. contentlength = Data. length;
Request. Timeout = 10000 ; // 10 s
String Content = String . Empty;
Try
{
// Write postdata into httpwebrequest
Using (Stream requeststream = Request. getrequeststream ())
{
Requeststream. Write (data, 0 , Data. Length );
Requeststream. Close ();
}
// Get httpwebresponse
Using (Httpwebresponse response = (Httpwebresponse) request. getresponse ())
{
If (Response. statuscode = Httpstatuscode. OK)
{
Using (Stream stream = Response. getresponsestream ())
{
Using (Streamreader Reader = New Streamreader (stream, encoding. utf8 ))
{
Content = Reader. readtoend ();
Reader. Close ();
}
Stream. Close ();
}
}
Response. Close ();
}
}
Catch (System. net. webexception)
{
// Todo: consider adding logs with timeout exceptions here
}
Finally
{
If (Request ! = Null )
{
Request. Abort ();
Request = Null ;
}
}
Context. response. Write ( String . Format ( " Ipvssclient => {0} " , Content ));
}
}
We re-encapsulate all post data on the proxy and add new service verification and other data to post together to the real service interface. Finally, we return the call back to the proxy caller.
Of course, we can further encapsulate the post operation into a method.
Postrequest
Public Static String Postrequest ( String URL, Byte [] Data, Int Timeout)
{
// Httpwebrequest
Httpwebrequest request = (Httpwebrequest) httpwebrequest. Create (URL );
Request. Method = " Post " ;
Request. contenttype = " Application/X-WWW-form-urlencoded " ;
Request. contentlength = Data. length;
Request. Timeout = Timeout;
String Content = String . Empty;
Try
{
// Write postdata into httpwebrequest
Using (Stream requeststream = Request. getrequeststream ())
{
Requeststream. Write (data, 0 , Data. Length );
Requeststream. Close ();
}
// Get httpwebresponse
Using (Httpwebresponse response = (Httpwebresponse) request. getresponse ())
{
If (Response. statuscode = Httpstatuscode. OK)
{
Using (Stream stream = Response. getresponsestream ())
{
Using (Streamreader Reader = New Streamreader (stream, encoding. utf8 ))
{
Content = Reader. readtoend ();
Reader. Close ();
}
Stream. Close ();
}
}
Response. Close ();
}
}
Finally
{
If (Request ! = Null )
{
Request. Abort ();
Request = Null ;
}
}
Return Content;
}