2012-04-20 23 views
1

WebサービスにSOAPリクエストを送信したときに、この例外が発生しました。 "リモートサーバーがエラーを返しました:(500)Internal Server Error"。 WebServiceには2つのパラメータ(int a、int b)があります。私はそれらの2つのパラメータを削除する場合は、私は例外を取得しません。SOAPリクエストをWebServiceに送信するエラー500

これはSOAP + WSの初めての経験です。私は何を間違えますか? お時間を頂きましてありがとうございます。

編集: 私の大学は私のコードでエラーを見つけました。無効なヘッダーを付けて送信していました。ここで

が不足しているヘッダー部分である:

request.Headers.Add("SOAPAction", "http://tempuri.org/Add"); 

はまた、私が使用しているURLが間違っていました。 間違っ:正しい

http://localhost:62830/Service1.asmx/Add 

:ここ

http://localhost:62830/Service1.asmx 

は、ここでのWebService

private void button2_Click(object sender, EventArgs e) 
    { 

     var url = UrlTextBox.Text; 
     var xml_file_path = FileTextBox.Text; 

     XmlDocument xml_doc = new XmlDocument(); 
     xml_doc.Load(xml_file_path); 

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 

     request.Method = "POST"; 
     request.ContentType = "text/xml"; 
     request.Accept = "text/xml"; 
     request.Timeout = 30 * 1000; 

     Stream request_stream = request.GetRequestStream(); 
     xml_doc.Save(request_stream); 
     request_stream.Close(); 

     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
     Stream r_stream = response.GetResponseStream(); 

     StreamReader response_stream = new StreamReader(r_stream, System.Text.Encoding.Default); 
     string sOutput = response_stream.ReadToEnd(); 

     ResultTextBox.Text = sOutput; 

    } 

XMLファイルにSOAPを送る私のコード クライアントです

 <?xml version="1.0" encoding="utf-8"?> 
    <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> 
     <soap12:Body> 
     <add xmlns="http://tempuri.org/" > 
     <a>5</a> 
     <b>10</b> 
     </add> 
     </soap12:Body> 
    </soap12:Envelope> 

そしてここにWebServiceのコード

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
public class Service1 : System.Web.Services.WebService 
{ 

    [WebMethod] 
    public int Add(int a, int b) 
    { 
     return a + b; 
    } 
} 
+0

多分文字列としてそれらを読んで、その後、int型に変換することが –

+0

私の計画は、後でWebサービスへの項目の複雑なコレクションを送信することで役立つかもしれません。文字列としてそれらを読むことは、それらを複雑なクラスに変換する作業の多くであろう。 – Jack

答えて

0

手動でSOAPエンベロープを構築するのは良い考えではないようです。 WSDLを使用して "Web参照を追加"し、WCFに行く必要があります。

とにかく、ここでWSがローカルホストモードで(.asmxファイルに直接接続することにより、GETモードで動作する場合にも、あなたがチェックすることができ、私はweb.configファイル

<webServices> 
    <protocols> 
    <add name="HttpGet"/> 
    <add name="HttpPost"/> 
    </protocols> 
    <conformanceWarnings> 
    <remove name="BasicProfile1_1"/> 
    </conformanceWarnings> 
</webServices> 
</system.web> 

にWSに使用する構成の一部であります)

重要かどうかわかりませんが、封筒を少し違った形で書きました。 そして私はヘッダー "Content-Length"を追加しました。

<?xml version="1.0" encoding="utf-8"?> 
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> 
    <soap12:Body> 
    <a xmlns="http://tempuri.org/">5</a> 
    <b xmlns="http://tempuri.org/">10</b> 
    </soap12:Body> 
</soap12:Envelope> 
+0

あなたのヒントをありがとう。このコードは、既存のクライアント/ Webサービスアプリケーションの一部です。私はこれがあなたが提案したソリューションに簡単に変換できるかどうかは分かりません。 – Jack

関連する問題