我得到了“远程服务器返回错误:(400)错误请求。” 当我向 Quickbooks 添加新客户时.. 以下代码不起作用.. 为什么?
string Results = string.Empty;
string requestUrl = null;
requestUrl = "https://apps.quickbooks.com/j/AppGateway";
HttpWebRequest WebRequestObject = null;
StreamReader sr = null;
HttpWebResponse WebResponseObject = null;
StreamWriter stwr = null;
try
{
WebRequestObject = (HttpWebRequest)WebRequest.Create(requestUrl);
WebRequestObject.Method = "POST";
WebRequestObject.ContentType = "application/x-qbxml";
WebRequestObject.AllowAutoRedirect = false;
string post = @"<?xml version=""1.0"" encoding=""utf-8""?>
<?qbxml version=""6.0""?>
<QBXML>
<SignonMsgsRq>
<SignonDesktopRq>
<ClientDateTime>%%CLIENT_DATE_TIME%%</ClientDateTime>
<ApplicationLogin>mydvqbapp.www.ipx2.com</ApplicationLogin>
<ConnectionTicket>TGT-142-6jCkNfIvWB$7Aw_ppW14jA</ConnectionTicket>
<Language>English</Language>
<AppID>APP_ID</AppID>
<AppVer>1</AppVer>
</SignonDesktopRq>
</SignonMsgsRq>
<QBXMLMsgsRq onError=""stopOnError"">
<CustomerAddRq>
<CustomerAdd> <!-- required -->
<Name >Sample Name</Name> <!-- required -->
<IsActive >True</IsActive> <!-- optional -->
<ClassRef> <!-- optional -->
<ListID ></ListID> <!-- optional -->
<FullName ></FullName> <!-- optional -->
</ClassRef>
<ParentRef> <!-- optional -->
<ListID ></ListID> <!-- optional -->
<FullName ></FullName> <!-- optional -->
</ParentRef>
<CompanyName >Sample CompanyName</CompanyName> <!-- optional -->
<Salutation >Mr</Salutation> <!-- optional -->
<FirstName >Sam Firstname</FirstName> <!-- optional -->
<MiddleName ></MiddleName> <!-- optional -->
<LastName >Sam LastName</LastName> <!-- optional -->
<JobTitle ></JobTitle> <!-- optional -->
<BillAddress> <!-- optional -->
<Addr1 >Sam Add1</Addr1> <!-- optional -->
<Addr2 >Sam Add2</Addr2> <!-- optional -->
<Addr3 ></Addr3> <!-- optional -->
<Addr4 ></Addr4> <!-- optional -->
<Addr5 ></Addr5> <!-- optional -->
<City ></City> <!-- optional -->
<State ></State> <!-- optional -->
<PostalCode ></PostalCode> <!-- optional -->
<Country ></Country> <!-- optional -->
<Note ></Note> <!-- optional -->
</BillAddress>
<ShipAddress> <!-- optional -->
<Addr1 ></Addr1> <!-- optional -->
<Addr2 ></Addr2> <!-- optional -->
<Addr3 ></Addr3> <!-- optional -->
<Addr4 ></Addr4> <!-- optional -->
<Addr5 ></Addr5> <!-- optional -->
<City ></City> <!-- optional -->
<State ></State> <!-- optional -->
<PostalCode ></PostalCode> <!-- optional -->
<Country ></Country> <!-- optional -->
<Note ></Note> <!-- optional -->
</ShipAddress>
<ShipToAddress> <!-- must occur 0 - 50 times -->
<Name >Test</Name> <!-- required -->
<Addr1 ></Addr1> <!-- optional -->
<Addr2 ></Addr2> <!-- optional -->
<Addr3 ></Addr3> <!-- optional -->
<Addr4 ></Addr4> <!-- optional -->
<Addr5 ></Addr5> <!-- optional -->
<City ></City> <!-- optional -->
<State ></State> <!-- optional -->
<PostalCode ></PostalCode> <!-- optional -->
<Country ></Country> <!-- optional -->
<Note ></Note> <!-- optional -->
<DefaultShipTo ></DefaultShipTo> <!-- optional -->
</ShipToAddress>
<Phone ></Phone> <!-- optional -->
<AltPhone ></AltPhone> <!-- optional -->
<Fax ></Fax> <!-- optional -->
<Email ></Email> <!-- optional -->
<Cc ></Cc> <!-- optional -->
<Contact ></Contact> <!-- optional -->
<AltContact ></AltContact> <!-- optional -->
<AdditionalContactRef> <!-- must occur 0 - 8 times -->
<ContactName ></ContactName> <!-- required -->
<ContactValue ></ContactValue> <!-- required -->
</AdditionalContactRef>
<Contacts> <!-- optional, may repeat -->
<Salutation ></Salutation> <!-- optional -->
<FirstName ></FirstName> <!-- required -->
<MiddleName ></MiddleName> <!-- optional -->
<LastName ></LastName> <!-- optional -->
<JobTitle ></JobTitle> <!-- optional -->
<AdditionalContactRef> <!-- must occur 0 - 5 times -->
<ContactName ></ContactName> <!-- required -->
<ContactValue ></ContactValue> <!-- required -->
</AdditionalContactRef>
</Contacts>
<CustomerTypeRef> <!-- optional -->
<ListID ></ListID> <!-- optional -->
<FullName ></FullName> <!-- optional -->
</CustomerTypeRef>
<TermsRef> <!-- optional -->
<ListID ></ListID> <!-- optional -->
<FullName ></FullName> <!-- optional -->
</TermsRef>
<SalesRepRef> <!-- optional -->
<ListID ></ListID> <!-- optional -->
<FullName ></FullName> <!-- optional -->
</SalesRepRef>
<OpenBalance ></OpenBalance> <!-- optional -->
<OpenBalanceDate ></OpenBalanceDate> <!-- optional -->
<SalesTaxCodeRef> <!-- optional -->
<ListID ></ListID> <!-- optional -->
<FullName ></FullName> <!-- optional -->
</SalesTaxCodeRef>
<ItemSalesTaxRef> <!-- optional -->
<ListID ></ListID> <!-- optional -->
<FullName ></FullName> <!-- optional -->
</ItemSalesTaxRef>
<ResaleNumber ></ResaleNumber> <!-- optional -->
<AccountNumber ></AccountNumber> <!-- optional -->
<CreditLimit ></CreditLimit> <!-- optional -->
<PreferredPaymentMethodRef> <!-- optional -->
<ListID ></ListID> <!-- optional -->
<FullName ></FullName> <!-- optional -->
</PreferredPaymentMethodRef>
<CreditCardInfo> <!-- optional -->
<CreditCardNumber ></CreditCardNumber> <!-- optional -->
<ExpirationMonth ></ExpirationMonth> <!-- optional -->
<ExpirationYear ></ExpirationYear> <!-- optional -->
<NameOnCard ></NameOnCard> <!-- optional -->
<CreditCardAddress ></CreditCardAddress> <!-- optional -->
<CreditCardPostalCode ></CreditCardPostalCode> <!-- optional -->
</CreditCardInfo>
<!-- JobStatus may have one of the following values: Awarded, Closed, InProgress, None [DEFAULT], NotAwarded, Pending -->
<JobStatus ></JobStatus> <!-- optional -->
<JobStartDate ></JobStartDate> <!-- optional -->
<JobProjectedEndDate ></JobProjectedEndDate> <!-- optional -->
<JobEndDate ></JobEndDate> <!-- optional -->
<JobDesc ></JobDesc> <!-- optional -->
<JobTypeRef> <!-- optional -->
<ListID ></ListID> <!-- optional -->
<FullName ></FullName> <!-- optional -->
</JobTypeRef>
<Notes ></Notes> <!-- optional -->
<AdditionalNotes> <!-- optional, may repeat -->
<Note ></Note> <!-- required -->
</AdditionalNotes>
<!-- PreferredDeliveryMethod may have one of the following values: None [Default], Email, Fax -->
<PreferredDeliveryMethod >ENUMTYPE</PreferredDeliveryMethod> <!-- optional -->
<PriceLevelRef> <!-- optional -->
<ListID ></ListID> <!-- optional -->
<FullName ></FullName> <!-- optional -->
</PriceLevelRef>
<ExternalGUID ></ExternalGUID> <!-- optional -->
<CurrencyRef> <!-- optional -->
<ListID ></ListID> <!-- optional -->
<FullName ></FullName> <!-- optional -->
</CurrencyRef>
</CustomerAdd>
<IncludeRetElement ></IncludeRetElement> <!-- optional, may repeat -->
</CustomerAddRq>
</QBXMLMsgsRq>
</QBXML>";
post = post.Replace("%%CLIENT_DATE_TIME%%", DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"));
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(post);
post = xmlDoc.InnerXml;
WebRequestObject.ContentLength = post.Length;
stwr = new StreamWriter(WebRequestObject.GetRequestStream());
stwr.Write(post);
stwr.Close();
WebResponseObject = (HttpWebResponse)WebRequestObject.GetResponse();
sr = new StreamReader(WebResponseObject.GetResponseStream());
Results = sr.ReadToEnd();
}
finally
{
try
{
lblMethod.Text = Results;
sr.Close();
}
catch(Exception ex)
{
lblMethod.Text = "Error:<br/>" + ex.Message;
}
try
{
WebResponseObject.Close();
WebRequestObject.Abort();
lblMethod.Text = lblMethod.Text + "Sucessfully Connected";
}
catch (Exception ex)
{
lblMethod.Text = "Error:<br/>" + ex.Message;
}
}
}
在上面的编码中,我得到了一个错误位置,“WebResponseObject = (HttpWebResponse)WebRequestObject.GetResponse();”