-3

I'm trying to convert a previously written .net winform app from vb to c# and I'm running into trouble with a web send function. How can I convert this into c#?

Public Shared Function Send(p_ipAddress As String, p_action As String, p_page As String, p_body As String, p_filePath As String) As String

        Dim objHttp = CreateObject("MSXML2.ServerXMLHTTP")
        'objHttp.setTimeouts(1000, 1000, 1000, 1000)  '-- Timeout

        objHttp.Open(p_action, sUrl, False)

        If t_fileContent.Length > 0 Then
            objHttp.setRequestHeader("Content-Type", "multipart/form-data; boundary=" & t_multipart_boundary)
        Else
            t_fileContent.Append(p_body)
            objHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
        End If

        objHttp.Send(t_fileContent.ToString())

        If objHttp.Status = 200 Then
            Return objHttp.responseText
        End If

        Return ""

    End Function

The HTTP object is what I'm having trouble translating into c#. I dont' know whether I need to use a http client, http web request, I'm fairly new to web calls.

EDIT I've shortened the code to specifically what I'm not sure about, removing the fluff.

enter image description here

Drew Jackson
  • 9
  • 1
  • 13

2 Answers2

0

Try this:

public static string Send(string p_ipAddress, string p_action, string p_page, string p_body, string p_filePath)
{
    dynamic objHttp = Microsoft.VisualBasic.Interaction.CreateObject("MSXML2.ServerXMLHTTP");
    // objHttp.setTimeouts(1000, 1000, 1000, 1000)  '-- Timeout

    objHttp.Open(p_action, sUrl, false);

    if (t_fileContent.Length > 0)
        objHttp.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + t_multipart_boundary);
    else
    {
        t_fileContent.Append(p_body);
        objHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    }

    objHttp.Send(t_fileContent.ToString());

    if (objHttp.Status == 200)
        return objHttp.responseText;

    return "";
}

You have to add a reference to Microsoft.VisualBasic.dll.

But there are proper .NET classes that handle sending over HTTP. It would be better to rewrite using those.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
0

You don't have to use Microsoft.VisualBasic.dll - there is a 'core' .NET way to do this:

public static string Send(string p_ipAddress, string p_action, string p_page, string p_body, string p_filePath)
{
    dynamic objHttp = System.Activator.CreateInstance(System.Type.GetTypeFromProgID("MSXML2.ServerXMLHTTP"));

    objHttp.Open(p_action, sUrl, false);

    if (t_fileContent.Length > 0)
        objHttp.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + t_multipart_boundary);
    else
    {
        t_fileContent.Append(p_body);
        objHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    }

    objHttp.Send(t_fileContent.ToString());

    if (objHttp.Status == 200)
        return objHttp.responseText;

    return "";
}
Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28