6

アフィリエイト アカウントが 1 つあり、soapデータを取得するために電話をかける必要があります。あるサイトから準備完了のコードを入手して適用しようとしましたが、500(internal server error). 私のコードを以下に示します。

public void getdata()
{
    var _url = "http://secure.directtrack.com/api/soap_affiliate.php";
    var _action = "http://secure.directtrack.com/api/soap_affiliate.php/dailyStatsInfo";

    XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
    HttpWebRequest webRequest = CreateWebRequest(_url, _action);
    InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

    // begin async call to web request.
    IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

    // suspend this thread until call is complete. You might want to
    // do something usefull here like update your UI.
    asyncResult.AsyncWaitHandle.WaitOne();

    // get the response from the completed web request.
    string soapResult;
    using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
    {
        soapResult = rd.ReadToEnd();
    }
    Console.Write(soapResult);

}

private static HttpWebRequest CreateWebRequest(string url, string action)
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.Headers.Add("SOAPAction", action);
    webRequest.ContentType = "text/xml;charset=\"utf-8\"";
    webRequest.Accept = "text/xml";
    webRequest.Method = "POST";
    return webRequest;
}

private static XmlDocument CreateSoapEnvelope()
{
    XmlDocument soapEnvelop = new XmlDocument();

    soapEnvelop.LoadXml(@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/1999/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/1999/XMLSchema""><SOAP-ENV:Body  xmlns=""http://soapinterop.org//"" SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""> <q1:Execute xmlns:q1=""http://secure.directtrack.com/api/soap_affiliate.php/dailyStatsInfo""><client xsi:type=""xsd:string"">MyClientNAme</client><add_code xsi:type=""xsd:string"">MyCode</add_code><password xsi:type=""xsd:string"">MyPassword</password><program_id xsi:type=""xsd:int"">161</program_id></q1:Execute></SOAP-ENV:Body></SOAP-ENV:Envelope>");


    return soapEnvelop;
}

private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
    using (Stream stream = webRequest.GetRequestStream())
    {
        soapEnvelopeXml.Save(stream);
    }
}

何が問題ですか?前もって感謝します。

4

4 に答える 4

5

内部サーバー エラーは、エラーがサーバー側にあることを意味します。コードが正確にサービスを呼び出しているか、サーバーが処理方法を認識していないパラメーターを渡している可能性があります (ただし、サーバー コードはそのように通知するほどスマートではありません)。

サーバーとサーバーが何を期待しているかについて詳しく知らなければ、問題を診断することはできません。

そうは言っても、石鹸の封筒が問題である可能性があります. 正しいクライアント名、追加コード、パスワード、プログラム ID などを入力しましたか?

于 2013-04-19T14:07:01.370 に答える
4

私もこれを経験しました。このコードをどこで入手したかさえわかります:)

だからそれをチェックしてください

webRequest.Headers.Add("SOAPAction", action);

問題です

単に使用する

webRequest.Headers.Add("SOAP:Action");
于 2016-10-07T14:43:19.190 に答える
-2

内部サーバー エラーは未処理の例外の結果である可能性が高いため、コードを 1 つ以上の try-catch ブロックでラップしてみてください。

SOAP xml の形式が正しくない場合、この行は XmlException をスローします。

soapEnvelop.LoadXml(@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/1999/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/1999/XMLSchema""><SOAP-ENV:Body  xmlns=""http://soapinterop.org//"" SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""> <q1:Execute xmlns:q1=""http://secure.directtrack.com/api/soap_affiliate.php/dailyStatsInfo""><client xsi:type=""xsd:string"">MyClientNAme</client><add_code xsi:type=""xsd:string"">MyCode</add_code><password xsi:type=""xsd:string"">MyPassword</password><program_id xsi:type=""xsd:int"">161</program_id></q1:Execute></SOAP-ENV:Body></SOAP-ENV:Envelope>");

これは IOExceptions を生成する可能性があります:

soapEnvelopeXml.Save(stream);

おそらくもっとあるでしょうが、これはあなたに正しい方向への突っ込みを与えるはずです...

于 2013-04-19T12:47:04.127 に答える