2012-01-03 22 views
0

我有一個Java Servelet的像 http://152.252.271.12:9999/media/servlet/RequestProcessor.do如何調用Java Servelet並在asp.net中獲得響應?

爲了這個,我需要傳遞參數的查詢字符串這樣 http://152.252.271.12:9999/media/servlet/RequestProcessor.do?input=abc

我需要閱讀迴響應。


我試過這樣。

 request.Method = "POST"; 
     request.KeepAlive = false; 
     request.Credentials = CredentialCache.DefaultCredentials; 

      using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) 
      { 
       writer.Write(postData); 
      } 

      // Read Response. 

      HttpWebResponse webresponse = (HttpWebResponse)request.GetResponse(); 

      using (StreamReader reader = new StreamReader(webresponse.GetResponseStream(), Encoding.Default)) 
      { 
       result = reader.ReadToEnd(); 
       logtxn.LogTxn(result, "SPDCL RESPONSE"); 
      } 

      webresponse.Close(); 

知道的任何幫助..

+0

如果你只需要擁有響應使用Web客戶端[DownladString(http://msdn.microsoft.com/en-us/library/fhd1f0sw.aspx)方法。你給它的網址並取回迴應。 – 2012-01-03 11:22:16

回答

0

我以前有過類似的問題,我不得不在IIS中託管一個服務,該服務接受一個包含請求的字符串,並返回另一個字符串與該請求的響應。以上所有需要使用HTTP POST完成。

我找到的解決方案是在IIS上託管WCF服務並將其配置爲使用HTTP GET/POST協議。

另外我還記得我需要使用Stream類來輸入和輸出數據(在這種情況下,一個簡單的字符串不起作用),換句話說,我有一個WCF中的HTTP POST方法,類似於:

Stream GetData(Stream data); 

看看這個article

0

您可以使用WebClient類作爲servlet的客戶端。例如

WebClient myWebClient = new WebClient(); 

// Download the Web resource and save it into a data buffer. 
byte[] myDataBuffer = myWebClient.DownloadData (@"http://152.252.271.12:9999/media/servlet/RequestProcessor.do?input=abc"); 

// Display the downloaded data. 
string download = Encoding.ASCII.GetString(myDataBuffer); 

現在解析結果字符串download,將獲得的響應。

0

請找到下面的代碼:

public class MyWebRequest 
    { 
     private WebRequest request; 
     private Stream dataStream; 
     private string status; 
     public String Status 
     { 
      get 
      { 
       return status; 
      } 
      set 
      { 
       status = value; 
      } 
     } 
     public MyWebRequest(string url) 
     { 
      // Create a request using a URL that can receive a post. 
      request = WebRequest.Create(url); 
     } 
     public MyWebRequest(string url, string method): this(url) 
     { 
      if (method.Equals("GET") || method.Equals("POST")) 
      { 
       // Set the Method property of the request to POST. 
       request.Method = method; 
      } 
      else 
      { 
       throw new Exception("Invalid Method Type"); 
      } 
     } 
     public MyWebRequest(string url, string method, string data): this(url, method) 
     { 
      // Create POST data and convert it to a byte array. 
      string postData = data; 
      byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
      // Set the ContentType property of the WebRequest. 
      request.ContentType = "application/x-www-form-urlencoded"; 
      // Set the ContentLength property of the WebRequest. 
      request.ContentLength = byteArray.Length; 
      // Get the request stream. 
      dataStream = request.GetRequestStream(); 
      // Write the data to the request stream. 
      dataStream.Write(byteArray, 0, byteArray.Length); 
      // Close the Stream object. 
      dataStream.Close(); 
     } 
     public string GetResponse() 
     { 
      // Get the original response. 
      WebResponse response = request.GetResponse(); 
      this.Status = ((HttpWebResponse) response) 
       .StatusDescription; 
      // Get the stream containing all content returned by the requested server. 
      dataStream = response.GetResponseStream(); 
      // Open the stream using a StreamReader for easy access. 
      StreamReader reader = new StreamReader(dataStream); 
      // Read the content fully up to the end. 
      string responseFromServer = reader.ReadToEnd(); 
      // Clean up the streams. 
      reader.Close(); 
      dataStream.Close(); 
      response.Close(); 
      return responseFromServer; 
     } 
    } 




//create the constructor with post type and few data 
    MyWebRequest myRequest = new MyWebRequest("http://www.yourdomain.com", "POST", "a=value1&b=value2"); 
    //show the response string on the console screen. 
    Console.WriteLine(myRequest.GetResponse());