JS calls the cross-origin service through a service proxy

Source: Internet
Author: User
Tags call back
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.