gpt4 book ai didi

c# - 有人可以将 C# "HttpWebRequest"转换为 VB6 吗?

转载 作者:太空宇宙 更新时间:2023-11-03 23:12:24 24 4
gpt4 key购买 nike

我们正在尝试连接 API 以发布销售线索并执行一些其他功能。我们正在使用 VB6,不幸的是,我们没有从 API 人员那里得到太多帮助。

他们向我们发送了一段用 C# 编写的代码。我们需要像他们一样做,但在 VB6 中。我们不能使用“HttpWebRequest”,因为我们知道它不适用于 VB6。

这是我们收到的代码:

public class LeadField
{
public string FieldCode;
public string FieldValue;
}
public class aLink
{
public string href;
public string methods;
public string rel = "lead";
public string description = "Get, update, or delete the lead";
}
public class ReturnLead
{
public string message;
public LeadField[] detail;
public aLink[] links;
}

if (objSales.Count == 1)
{
ArrayList objLeadField = new ArrayList();
csDealSheet objDealSheet = objSales.First();

objLeadField.Add(new LeadField() { FieldCode = "AgentCode", FieldValue = objDealSheet.DataObject.REP.ToString() });
objLeadField.Add(new LeadField() { FieldCode = "VendorCode", FieldValue = "FL 01" });
objLeadField.Add(new LeadField() { FieldCode = "CompanyName", FieldValue = objDealSheet.DataObject.COMPANY });
objLeadField.Add(new LeadField() { FieldCode = "AccountNumber", FieldValue = objDealSheet.DataObject.Account.ToUpper() });
objLeadField.Add(new LeadField() { FieldCode = "ContactFirstName", FieldValue = objDealSheet.DataObject.FirstName });
objLeadField.Add(new LeadField() { FieldCode = "ContactLastName", FieldValue = objDealSheet.DataObject.LastName });
objLeadField.Add(new LeadField() { FieldCode = "PrimaryPhone", FieldValue = objDealSheet.DataObject.PHONE10 });
//objLeadField.Add(new LeadField() { FieldCode = "LanguageCode", FieldValue = objDealSheet.DataObject.LANGUAGE.Replace("E", "English").Replace("S", "Spanish") });
objLeadField.Add(new LeadField() { FieldCode = "NumberOfAccounts", FieldValue = "1" });
objLeadField.Add(new LeadField() { FieldCode = "ProductTypeCode", FieldValue = objDealSheet.DataObject.Commodity });
objLeadField.Add(new LeadField() { FieldCode = "ProviderCode", FieldValue = objDealSheet.DataObject.UTILITY.ToUpper()});
objLeadField.Add(new LeadField() { FieldCode = "AccountHolderFullName", FieldValue = objDealSheet.DataObject.FirstName + " " + objDealSheet.DataObject.LastName });
objLeadField.Add(new LeadField() { FieldCode = "ProductCode", FieldValue = objDealSheet.DataObject.PROGRAM });
objLeadField.Add(new LeadField() { FieldCode = "ServiceAddress1", FieldValue = objDealSheet.DataObject.ST_NUMB + " " + objDealSheet.DataObject.ADDR });
objLeadField.Add(new LeadField() { FieldCode = "ServiceAddress2", FieldValue = objDealSheet.DataObject.UNIT });
objLeadField.Add(new LeadField() { FieldCode = "ServiceAddressCity", FieldValue = objDealSheet.DataObject.CITY });
objLeadField.Add(new LeadField() { FieldCode = "ServiceAddressPostalCode", FieldValue = objDealSheet.DataObject.ZIP });
objLeadField.Add(new LeadField() { FieldCode = "ServiceAddressState", FieldValue = objDealSheet.DataObject.ST });
objLeadField.Add(new LeadField() { FieldCode = "ServiceAddressCountry", FieldValue = objDealSheet.DataObject.COUNTRY.Replace("US", "USA") });
objLeadField.Add(new LeadField() { FieldCode = "BillingAddress1", FieldValue = objDealSheet.DataObject.BILLNUMB + " " + objDealSheet.DataObject.BILLADDR });
objLeadField.Add(new LeadField() { FieldCode = "BillingAddress2", FieldValue = objDealSheet.DataObject.BILLUNIT });
objLeadField.Add(new LeadField() { FieldCode = "BillingAddressCity", FieldValue = objDealSheet.DataObject.BILLCITY });
objLeadField.Add(new LeadField() { FieldCode = "BillingAddressPostalCode", FieldValue = objDealSheet.DataObject.BILLZIP });
objLeadField.Add(new LeadField() { FieldCode = "BillingAddressState", FieldValue = objDealSheet.DataObject.BILLST });
objLeadField.Add(new LeadField() { FieldCode = "BillingAddressCountry", FieldValue = objDealSheet.DataObject.BILLCOUNTR.Replace("US", "USA") });

//Send one lead
StringBuilder sb = new StringBuilder();
new JavaScriptSerializer().Serialize(objLeadField, sb);
string format = sb.ToString();
format = "[" + format.Substring(1, format.Length - 2) + "]";
}


//Create the web request


string url="https://www.dummyurl.com/leads";

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

request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
// Create a byte array of the data we want to send
byte[] byteData = UTF8Encoding.UTF8.GetBytes(Lead);

// Set the content length in the request headers
request.ContentLength = byteData.Length;
string Username=********;
string Password=********;
request.Headers.Add("userName",Username);
request.Headers.Add("password",Password);
}

// Write data
using (Stream postStream = request.GetRequestStream())
postStream.Write(byteData, 0, byteData.Length);

// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
// Save as string
string res = reader.ReadToEnd();
if (res.Length > 0)
{
List<ReturnLead> ServRes = new JavaScriptSerializer().Deserialize<List<ReturnLead>>(res);
ReturnLead lead = ServRes.First();
string LeadID = lead.links[0].href.Substring(lead.links[0].href.LastIndexOf("/") + 1, lead.links[0].href.Length - lead.links[0].href.LastIndexOf("/") - 1);
}
}

我们一直在查看大量 VB6/JSON/POST 请求,并能够将一些东西放在一起,但它几乎没有工作。我们以前从未在 VB6 中这样做过,也不精通 C#。

只是想知道是否有人可以给我们一个开始或一些 VB6 方向来移植这个。

谢谢!

最佳答案

这是 VBScript,因此您可以粘贴到 VB6/VBA 中。这使用 Microsoft.XMLHTTP 对象。您还可以编写 Internet Explorer 脚本(或使 IE 成为您应用程序的一部分)。参见 https://msdn.microsoft.com/en-us/library/ms537505(v=vs.85).aspx获取 XMLHTTP 帮助。

这将从 MS 网站下载适用于 Vista 32 位的安全扫描器。

Set fso = CreateObject("Scripting.FileSystemObject")
Set Outp = Wscript.Stdout
Set wshShell = CreateObject("Wscript.Shell")
Set ShApp = CreateObject("Shell.Application")
On Error Resume Next
Set File = WScript.CreateObject("Microsoft.XMLHTTP")
File.Open "GET", "http://definitionupdates.microsoft.com/download/definitionupdates/safetyscanner/x86/msert.exe:200", False
File.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 1.1.4322; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C; .NET4.0E; BCD2000; BCD2000)"
File.Send
If err.number <> 0 then
wscript.echo ""
wscript.echo "Error getting file"
wscript.echo "=================="
wscript.echo ""
wscript.echo "Error " & err.number & "(0x" & hex(err.number) & ") " & err.description
wscript.echo "Source " & err.source
wscript.echo ""
wscript.echo "HTTP Error " & File.Status & " " & File.StatusText
wscript.echo File.getAllResponseHeaders
else
On Error Goto 0
Set BS = CreateObject("ADODB.Stream")
BS.type = 1
BS.open
BS.Write File.ResponseBody
BS.SaveToFile ShApp.Namespace(&h10).self.path & "\safetyscanner.exe", 2
wshshell.Run "c:\users\safetyscanner.exe", 1, False
End If

关于c# - 有人可以将 C# "HttpWebRequest"转换为 VB6 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38645183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com