WCF Safety Certification: SoapHeader (b) calls using HTTP Request

WCF Safety Certification: SoapHeader (b) calls using HTTP Request


In the front end of the first article WinFrom Client program presentation to be produced when the SoapHeader delivery request, and the other written ClientHeader a library, and must be added in the configuration corresponding to the setting, the procedures seem complicated. In fact, sometimes hope is WebRequest native format combination Soap own data to exchange data.

Before you start you need to know some of the rules of data, first by the wsdl specification can be found SOAPAction .

soapaction

In the foregoing described process using Fiddler observation data, see Soap fed standard data format, which is now in the program string SOLE combined, which contains the SoapHeader, the parameters of the method to be invoked passed.

request_1

   1:  
 
 
   2:  
 
 
   3:      
 
 
  
  account number
 
 
   4:      
 
 
  
  password
 
 
   5:    
   6:    
 
 
   7:      
 
 
   8:        
 
 
  
  test
 
 
   9:      
  10:    
  11:  

With SoapAction Soap format and data, then three ways to use the platform to write.

1. .Net is called by

   1:  string wsdlUri = "http://kevin-h:905/basicHttpSoapHeader.host/MyProducts.svc?singleWsdl";
   2:  string username = System.Configuration.ConfigurationManager.AppSettings["username"].ToString();
   3:  string pwd = System.Configuration.ConfigurationManager.AppSettings["pwd"].ToString();
   4:   
   5:  private void btnCallSaySomething_Click(object sender, EventArgs e) {
   6:      try {
   7:          HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(wsdlUri);
   8:          webRequest.Method = "POST";
   9:          webRequest.ContentType = "text/xml;charset=UTF-8";
  10:                  webRequest.Headers.Add("SOAPAction:"http://tempuri.org/IProductService/SaySomething"");  //<--SOAPAction 别忘了
  11:          StringBuilder soapStr = new StringBuilder(Environment.NewLine);
  12:          soapStr.Append("
 
 
  
  " + Environment.NewLine);
 
 
  13:          soapStr.Append("  
 
 
  
  " + Environment.NewLine);
 
 
  14:          soapStr.Append("    
 
 
  
  "+username+"
 
 " + Environment.NewLine);
  15:          soapStr.Append("    
 
 
  
  "+pwd+"
 
 " + Environment.NewLine);
  16:          soapStr.Append("  " + Environment.NewLine);
  17:          soapStr.Append("  
 
 
  
  " + Environment.NewLine);
 
 
  18:          soapStr.Append("    
 
 
  
  " + Environment.NewLine);
 
 
  19:          soapStr.Append("      
 
 
  
  test
 
 " + Environment.NewLine);
  20:          soapStr.Append("    " + Environment.NewLine);
  21:          soapStr.Append("  " + Environment.NewLine);
  22:          soapStr.Append("" + Environment.NewLine);
  23:   
  24:          byte[] buffer = Encoding.UTF8.GetBytes(soapStr.ToString());
  25:          webRequest.ContentLength = buffer.Length;
  26:          Stream post = webRequest.GetRequestStream();
  27:          post.Write(buffer, 0, buffer.Length);
  28:          post.Close();
  29:   
  30:          HttpWebResponse response;
  31:          response = (HttpWebResponse)webRequest.GetResponse();
  32:          Stream respostData = response.GetResponseStream();
  33:          StreamReader sr = new StreamReader(respostData);
  34:          string strResponseXml = sr.ReadToEnd();
  35:   
  36:          txtOutputValue.Text = strResponseXml;
  37:      } catch (Exception er) {
  38:          if (er.InnerException != null) {
  39:              txtSaySomethingMsg.Text = "InnerException:" + er.InnerException.Message;
  40:          } else {
  41:              txtSaySomethingMsg.Text = "Exception:" + er.Message;
  42:          }
  43:      }
  44:  }

After the above results of the implementation of the program obtained the following data in XML format, as long as Parse this data you can get what you want.

   1:  
 
 
   2:  
 
 
   3:      
 
 
   4:          
 
 
  
  You say [test].
 
 
   5:      
   6:  
   7:  

2. Java is called by

   1:  package wcf.com;
   2:   
   3:  import java.io.BufferedReader;
   4:  import java.io.ByteArrayOutputStream;
   5:  import java.io.InputStreamReader;
   6:  import java.io.OutputStream;
   7:  import java.net.HttpURLConnection;
   8:  import java.net.URL;
   9:  import java.net.URLConnection;
  10:   
  11:  public class wcfClient {
  12:      public static void main(String[] args) {
  13:          String responseString = "";
  14:          String outputString = "";
  15:          String wsURL = "http://kevin-h:905/basicHttpSoapHeader.host/MyProducts.svc?wsdl";
  16:   
  17:          try{
  18:              URL url = new URL(wsURL);
  19:              URLConnection connection = url.openConnection();
  20:              HttpURLConnection httpConn = (HttpURLConnection)connection;
  21:              ByteArrayOutputStream bout = new ByteArrayOutputStream();
  22:              
  23:              StringBuilder xmlInput=new StringBuilder();
  24:              xmlInput.append("
 
 
  
  n");
 
 
  25:              xmlInput.append("  
 
 
  
  n");
 
 
  26:              xmlInput.append("    
 
 
  
  testman
 
 n");
  27:              xmlInput.append("    
 
 
  
  a123456
 
 n");
  28:              xmlInput.append("  n");
  29:              xmlInput.append("  
 
 
  
  n");
 
 
  30:              xmlInput.append("    
 
 
  
  n");
 
 
  31:              xmlInput.append("      
 
 
  
  Text test
 
 n");
  32:              xmlInput.append("    n");
  33:              xmlInput.append("  n");
  34:              xmlInput.append("n");
  35:              
  36:              byte[] buffer=new String(xmlInput).getBytes();
  37:              bout.write(buffer);
  38:              byte[] b = bout.toByteArray();
  39:              String SOAPAction ="http://tempuri.org/IProductService/SaySomething";
  40:              
  41: Http // set the appropriate parameters
  42:              httpConn.setRequestProperty("Content-Length",String.valueOf(b.length));
  43:              httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
  44:              httpConn.setRequestProperty("SOAPAction", SOAPAction);
  45:              httpConn.setRequestMethod("POST");
  46:              httpConn.setDoOutput(true);
  47:              httpConn.setDoInput(true);
  48:              OutputStream out = httpConn.getOutputStream();
  49:              out.write(b);
  50:              out.close();
  51:   
  52:              InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
  53:              BufferedReader in = new BufferedReader(isr);
  54:           
  55:              while ((responseString = in.readLine()) != null) {
  56:                  outputString = outputString + responseString;
  57:              }
  58:   
  59:              
  60:              System.out.println(outputString);
  61:          }catch(Exception er){
  62: er.printStackTrace ();
  63:          }
  64:      }
  65:  }

After performing the same result can be seen that the results of the previous output .Net obtained a self took will parse XML.

Ps. When using eclipse compose, send request to take back and to the right data, but later passed after the value has been changed to Chinese failure. Intuition is a coding problem, check for a long time only to find, eclipse default file format encoded MS950 in the windows when the archive file, if you change the file saved as UTF-8 Chinese problem is solved passed.

3. Android is called by

The most popular mobile devices, of course, must be able to call a WCF job, the following model is based on Android 4.2.2 version, for example.

After Android 3.0 can not directly network activity on the main thread, otherwise you will get NetworkOnMainThreadException error message. So here first create a SoapObjec of the class, and then to another Thread to execute on the main thread.

SoapObject.java

   1:  package com.example.callwcftest;
   2:   
   3:  import java.io.IOException;
   4:   
   5:  import org.apache.http.HttpEntity;
   6:  import org.apache.http.HttpResponse;
   7:  import org.apache.http.client.ClientProtocolException;
   8:  import org.apache.http.client.ResponseHandler;
   9:  import org.apache.http.client.methods.HttpPost;
  10:  import org.apache.http.entity.ByteArrayEntity;
  11:  import org.apache.http.impl.client.DefaultHttpClient;
  12:  import org.apache.http.params.HttpConnectionParams;
  13:  import org.apache.http.params.HttpParams;
  14:  import org.apache.http.params.HttpProtocolParams;
  15:  import org.apache.http.util.EntityUtils;
  16:   
  17:  import android.util.Log;
  18:   
  19:  public class SoapObject {
  20:      private String wsURL="";
  21:      private String soapAction="";
  22:      private String soapBody="";
  23:      
  24:      public void setWsURL(String wsURL){
  25:          this.wsURL=wsURL;
  26:      }
  27:      
  28:      public void setSoapAction(String soapAction){
  29:          this.soapAction=soapAction;
  30:      }
  31:      
  32:      public void setSoapBody(String soapBody){
  33:          this.soapBody=soapBody;
  34:      }
  35:   
  36:      public String sendRequest(){
  37:          String responseString="";
  38:          final DefaultHttpClient httpClient=new DefaultHttpClient();
  39: // request parameters
  40:            HttpParams params = httpClient.getParams();
  41:            HttpConnectionParams.setConnectionTimeout(params, 10000);
  42:            HttpConnectionParams.setSoTimeout(params, 15000);
  43:          // set parameter
  44:            HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);
  45:   
  46:            // POST the envelope
  47:            HttpPost httppost = new HttpPost(this.wsURL);
  48:            // add headers
  49:            httppost.setHeader("soapaction", this.soapAction);
  50:            httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
  51:            
  52:          try {
  53:              // the entity holds the request
  54:              // HttpEntity entity = new StringEntity(envelope);
  55:              HttpEntity entity = new ByteArrayEntity(this.soapBody.getBytes("UTF-8"));
  56:              httppost.setEntity(entity);
  57:   
  58:              // Response handler
  59:              ResponseHandler
 
 
  
   rh = new ResponseHandler
  
  
   
   () {
  
  
 
 
  60:                  // invoked when client receives response
  61:                  public String handleResponse(HttpResponse response)
  62:                          throws ClientProtocolException, IOException {
  63:                      // get response entity
  64:                      HttpEntity entity = response.getEntity();
  65:   
  66:                      // read the response as byte array
  67:                      StringBuffer out = new StringBuffer();
  68:                      byte[] b = EntityUtils.toByteArray(entity);
  69:   
  70:                      // write the response byte array to a string buffer
  71:                      out.append(new String(b, 0, b.length));
  72:                      return out.toString();
  73:                  }
  74:              };
  75:   
  76:              responseString = httpClient.execute(httppost, rh);
  77:          } catch (Exception e) {
  78:              Log.v("exception", e.toString());
  79:          }
  80:   
  81:          // close the connection
  82:          httpClient.getConnectionManager().shutdown();
  83:          
  84:          return responseString;
  85:      }
  86:  }

Then compose the main thread section

   1:  package com.example.callwcftest;
   2:   
   3:  import android.app.Activity;
   4:  import android.os.Bundle;
   5:  import android.util.Log;
   6:  import android.view.Menu;
   7:  import android.view.View;
   8:  import android.widget.Button;
   9:  import android.widget.TextView;
  10:   
  11:  public class MainActivity extends Activity {
  12:   
  13:      Button btnCallWcf;
  14:      TextView txtMsg;
  15:      @Override
  16:      protected void onCreate(Bundle savedInstanceState) {
  17:          super.onCreate(savedInstanceState);
  18:          setContentView(R.layout.activity_main);
  19:          
  20:          btnCallWcf=(Button)this.findViewById(R.id.btnCallWcf);
  21:          txtMsg=(TextView)this.findViewById(R.id.txtMsg);
  22:          
  23:          btnCallWcf.setOnClickListener(WcfCall);
  24:      }
  25:      
  26:      private Button.OnClickListener WcfCall=new Button.OnClickListener(){
  27:   
  28:          @Override
  29:          public void onClick(View arg0) {
  30:              runOnUiThread(new Runnable() {
  31:                  @Override
  32:                  public void run() {
  33:                      try{
  34:                          String wsURL="http://xxx.xxx.xxx.xxx:905/basicHttpSoapHeader.host/MyProducts.svc?singleWsdl";
  35:                          String soapAction="http://tempuri.org/IProductService/SaySomething";
  36:                          StringBuilder soapBody=new StringBuilder();
  37:                          soapBody.append("
 
 
  
  n");
 
 
  38:                          soapBody.append("  
 
 
  
  n");
 
 
  39:                          soapBody.append("    
 
 
  
  testman
 
 n");
  40:                          soapBody.append("    
 
 
  
  a123456
 
 n");
  41:                          soapBody.append("  n");
  42:                          soapBody.append("  
 
 
  
  n");
 
 
  43:                          soapBody.append("    
 
 
  
  n");
 
 
  44:                          soapBody.append("      
 
 
  
  Chinese & amp; english test
 
 n");
  45:                          soapBody.append("    n");
  46:                          soapBody.append("  n"); 
  47:                          soapBody.append("n");
  48:                          
  49:                          SoapObject client=new SoapObject();
  50:                          client.setWsURL(wsURL);
  51 client.setSoapAction (soapAction);
  52 client.setSoapBody (soapBody.toString ());
  53:                          
  54:                          txtMsg.setText(client.sendRequest());
  55:                          Log.v("success", txtMsg.getText().toString());
  56:                          }catch(Exception er){
  57:                              Log.d("MAIN err", er.getMessage());
  58:                          }
  59:                      
  60:                  }
  61:              });
  62:              
  63:          }
  64:          
  65:      };
  66:   
  67:  }

Implementation of the results

android_2

With these practices, we should be able to cope with the situation a lot.

Original: Big Box  WCF security authentication: SoapHeader (b) calls using HTTP Request


Guess you like

Origin www.cnblogs.com/petewell/p/11516574.html