简体   繁体   中英

Converting vb function to c# in winform app

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.

在此处输入图片说明

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM