luci5r
luci5r

Reputation: 635

Can someone convert C# "HttpWebRequest" to VB6?

We're trying connect with an API to post leads & perform some other functions. We're using VB6 and unfortunately we're not getting much assistance from the people with the API.

They sent us a code written in C#. We need to do the same as they are doing, but in VB6. We can't use the "HttpWebRequest" because from we understand it doesn't work with VB6.

This is the code we received:

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);
}
}

We've been taking a look at a lot of VB6/JSON/POST requests and were able to put something together but it didn't even nearly work. We've never done this before in VB6 and are not versed in C#.

Just wondering if someone can give us a start or some VB6 direction to port this over.

Thanks!

Upvotes: 1

Views: 1150

Answers (2)

luci5r
luci5r

Reputation: 635

Thanks for your responses guys, and thanks Noodles, for posting the code. Although not a direct conversion of the C# script we had originally obtained, we did manage to write code in VB6 to do what we needed to do.

Essentially we're using the MSXML2 and it's ServerXMLHTTP to setup a web request. We're able to login, POST data and retrieve the response. We do have some more fiddling to do in order to post Array data, as opposed to a single JSON string data, but we've gotten this far and are pretty hopeful we'll get further.

Following is the code we wrote which pretty much does what the C# code was doing, in case it's helpful:

Dim sUrl As String
Dim Xmlhttp As New MSXML2.ServerXMLHTTP60

strJSONToSend = "[{""FieldCode"": ""AccountHolderFirstName"",""FieldValue"": ""John""}]"

sUrl = "https://www.dummyurl.com/lead"
Xmlhttp.open "POST", sUrl, False
Xmlhttp.setRequestHeader "userName", "****"
Xmlhttp.setRequestHeader "password", "****"
Xmlhttp.setOption 2, SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS
Xmlhttp.setRequestHeader "Content-Type", "application/json; charset=utf-8"

Xmlhttp.send strjsontosend
domResponse = Xmlhttp.responseText
Text1.Text = Xmlhttp.responseText

Thanks Guys!

Upvotes: 2

user6017774
user6017774

Reputation:

This is VBScript so you can paste into VB6/VBA. This uses Microsoft.XMLHTTP object. You could also have scripted Internet Explorer (or made IE a part of your app). See https://msdn.microsoft.com/en-us/library/ms537505(v=vs.85).aspx for XMLHTTP help.

This downloads the safety scanner from MS web site for Vista 32 bit.

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

Upvotes: 2

Related Questions