1

我正在尝试使用服务器到服务器 XML API 使用 c#.net 3.5 进行谷歌结帐。

当我尝试发布我的 URL “远程服务器返回错误:(400) 错误请求”时,我收到以下错误。即使我确定我已经完全按照他们的指示进行了操作。我的代码在下面...帮助任何人?

    public void DoWebRequest(string content)
    {
        //Save the content string for later use
        _checkoutString = content;

        // Create a new request to the mentioned URL.    
        WebRequest myWebRequest = WebRequest.Create("https://sandbox.google.com/checkout/api/checkout/v2/merchantCheckout/Merchant/" + Utils.MerchantData.Id);

        //Create authorisation key
        string encodeKey = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(Utils.MerchantData.Id + ":" + Utils.MerchantData.Key));

        // Create an instance of the RequestState and assign 'myWebRequest' to it's request field.    
        RequestState myRequestState = new RequestState();
        myRequestState.request = myWebRequest;
        myWebRequest.ContentType = "application/xml; charset=UTF-8";
        //myWebRequest.Accept = 
        //myWebRequest.Headers["Accept"] = "application/xml; charset=UTF-8";
        ((HttpWebRequest)myWebRequest).Accept = "application/xml; charset=UTF-8";
        myWebRequest.Headers.Add("Authorization", "Basic " + encodeKey);

        // Set the 'Method' property  to 'POST' to post data to a Uri.
        myRequestState.request.Method = "POST";

        // Start the Asynchronous 'BeginGetRequestStream' method call.    
        IAsyncResult r = (IAsyncResult)myWebRequest.BeginGetRequestStream(new AsyncCallback(ReadCallback), myRequestState);

        // Pause the current thread until the async operation completes.
        _allDone.WaitOne();

        // Assign the response object of 'WebRequest' to a 'WebResponse' variable.
        WebResponse myWebResponse = myWebRequest.GetResponse();
        Stream streamResponse = myWebResponse.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);
        Char[] readBuff = new Char[256];
        int count = streamRead.Read(readBuff, 0, 256);

        while (count > 0)
        {
            String outputData = new String(readBuff, 0, count);
            Debug.Write(outputData);
            count = streamRead.Read(readBuff, 0, 256);
        }

        // Close the Stream Object.
        streamResponse.Close();
        streamRead.Close();

        // Release the HttpWebResponse Resource.
        myWebResponse.Close();      
    }

    private static void ReadCallback(IAsyncResult asynchronousResult)
    {
        RequestState myRequestState = (RequestState)asynchronousResult.AsyncState;
        WebRequest myWebRequest = myRequestState.request;

        // End the Asynchronus request.
        Stream streamResponse = myWebRequest.EndGetRequestStream(asynchronousResult);

        // Convert the string into a byte array.
        byte[] byteArray = Encoding.UTF8.GetBytes(_checkoutString);

        // Write the data to the stream.
        streamResponse.Write(byteArray, 0, _checkoutString.Length);
        streamResponse.Close();
        _allDone.Set();
    }

我在以下行收到错误:

        WebResponse myWebResponse = myWebRequest.GetResponse();

我的“内容”字符串如下:

            string googleCheckoutString =
            "<!-- Sell physical goods with tax and shipping -->" +
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<checkout-shopping-cart xmlns=\"http://checkout.google.com/schema/2\">" +
            "<shopping-cart>" +
            "<items>" +
            products +
            "</items>" +
            "</shopping-cart>" +
            "<checkout-flow-support>" +
            "<merchant-checkout-flow-support>" +
            "<shipping-methods>" +
            shipping +
            "</shipping-methods>" +
            "</merchant-checkout-flow-support>" +
            "</checkout-flow-support>" +
            "</checkout-shopping-cart>";
4

0 回答 0