Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All,

My Code here.
C#
string uriString = "http://www.Testcom";
WebClient myWebClient = new WebClient();
string postData = "data";
myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");
Console.WriteLine(myWebClient.Headers.ToString());
byte[] byteArray = Encoding.ASCII.GetBytes(postData);        
byte[] responseArray = myWebClient.UploadData(new 
Uri(uriString),"POST",byteArray);

Now I call UploadData and get method created in my API project something like this.
C#
[HttpPost]
[Route("doc2pdf")]
public HttpResponseMessage doc2pdf(byte[] fileContent)
{
    string pdfContent = string.Empty;
    //if(string.IsNullOrEmpty(docContent))
    //{
    //    var resp = Request.CreateResponse(HttpStatusCode.BadRequest,"Document content is empty.");
    //    return resp;
    //}
    if(fileContent != null || fileContent.Length > 0)
    {
        ..logic here
    }
}

The problem is always fileContent get {byte[0]}.

[^]

Now, how do I read the HTTP output?

What I have tried:

C#
WebClient myWebClient = new WebClient();
string postData = "data";
myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");
Console.WriteLine(myWebClient.Headers.ToString());

byte[] byteArray = Encoding.ASCII.GetBytes(postData);        
byte[] responseArray = myWebClient.UploadData(new Uri(uriString),"POST",byteArray);

Encoding.ASCII.GetString(responseArray);
Posted
Updated 29-Aug-17 23:33pm
v2
Comments
Graeme_Grant 30-Aug-17 5:16am    
What response code do you get?
.net developer123456789 30-Aug-17 5:18am    
Code here :- if(fileContent != null && fileContent.Length > 0)
{
//byte[] fileContent = Convert.FromBase64String(docContent);
MemoryStream ms = new MemoryStream(fileContent);
Document doc = new Document(ms);
ms.Close();

MemoryStream toMS = new MemoryStream();
doc.Save(toMS,SaveFormat.Pdf);
toMS.Position = 0;

byte[] pdfbytes = toMS.ToArray();
pdfContent = Convert.ToBase64String(pdfbytes);

return Request.CreateResponse(HttpStatusCode.OK,pdfContent);
}
Graeme_Grant 30-Aug-17 5:29am    
No, i am not asking for your code, I am asking for the "response code" from the server that you are talking to.
Graeme_Grant 30-Aug-17 5:43am    
When you talk to a server, there is a response that is made up of a header and a body. the response will either contain a 200/201 ok response or it will respond with a 4xx or 5xx response where 4xx are client errors like 404 "not found" or 400 "Bad Request" & 5xx are server errors like 500 "Internal server error" or 503 "Server unavailable". You can read more about them here: HTTP Status Codes[^]

So I ask you again, what response code are you getting?
.net developer123456789 30-Aug-17 5:52am    
Thanks,HttpStatusCode.OK =200/201

1 solution

I've put together a test solution for you to demonstrate how to do client comms with a WebAPI rest service.

The sample console client demonstrates:
* How to send a request
* how to read the response code & data
* how to post multi-part form post data

The sample Web Api Server controller demonstrates:
* how to handle multi-part form post received
* how to return a response to the client caller

The client console app:
C#
internal static class Program
{
    private static void Main()
    {
        UserWaiting();
        Console.WriteLine("Good TestGetMethod....");
        TestGetMethod("api/values");

        Console.WriteLine("Bad TestGetMethod....");
        TestGetMethod("api/valuesx");

        UserWaiting();
        Console.WriteLine("Good TestPostMethod....");
        TestPostMethod("api/values", new Dictionary<string, string> { ["valueField"] = "test post value" });

        Console.WriteLine("Bad TestPostMethod....");
        TestPostMethod("api/values", null);

        Console.WriteLine("\n-- DONE --");
        Console.ReadKey();
    }

    private const string host = "http://localhost:65189";

    private static void TestGetMethod(string path)
    {
        var response = Request(new Uri($"{host}/{path}", UriKind.Absolute),
                                "GET");

        var ms = GetResponseStream(response);

        if (response.StatusCode == HttpStatusCode.OK)
        {
            string responseData = GetResponseString(ms);
            ReportResponse(response, responseData);
        }
        else
        {
            ReportError(response, ms);
        }
    }

    private static void TestPostMethod(string path, Dictionary<string, string> values)
    {
        var response = Request(new Uri($"{host}/{path}", UriKind.Absolute),
                                "POST",
                                values);

        var ms = GetResponseStream(response);

        if (response.StatusCode == HttpStatusCode.OK)
        {
            string responseData = GetResponseString(ms);
            ReportResponse(response, responseData);
        }
        else
        {
            ReportError(response, ms);
        }
    }

    private static void UserWaiting()
    {
        Console.WriteLine("\n-- Waiting to begin --\n");
        Console.ReadKey();
    }

    private static void ReportError(HttpWebResponse response, MemoryStream ms)
    {
        if (ms != null)
        {
            string responseData = GetResponseString(ms);
            ReportResponse(response, responseData);
        }
        else
        {
            Console.WriteLine("!! UNHANDLED ERROR ENCOUNTERED");
        }
    }

    private static void ReportResponse(HttpWebResponse response, string responseData, bool isError = false)
    {
        Console.WriteLine();
        Console.WriteLine($"{(isError ? "!!" : "**")} {(int)response.StatusCode} - {response.StatusDescription}\n[{responseData}]");
        Console.WriteLine();
    }

    private static string GetResponseString(MemoryStream ms)
    {
        string responseData;
        using (var reader = new StreamReader(ms, Encoding.ASCII))
            responseData = reader.ReadToEnd();
        return responseData;
    }

    private static MemoryStream GetResponseStream(HttpWebResponse response)
    {
        MemoryStream ms = null;

        if (response != null)
        {
            using (var responseStream = response.GetResponseStream())
            {
                ms = new MemoryStream();
                responseStream.CopyTo(ms);
                ms.Position = 0;
            }
        }

        return ms;
    }

    private const string seperatorLine = "--";
    private const string terminateMarker = "--";

    private static HttpWebResponse Request(Uri uri, string method = "GET", Dictionary<string, string> parameters = null)
    {
        HttpWebResponse response = null;
        try
        {
            var clientRequest = WebRequest.Create(uri) as HttpWebRequest;
            clientRequest.Method = method;

            if (method == "POST" && parameters != null)
            {
                // post data
                var postdata = BuldPostData(parameters, clientRequest);

                using (var requestStream = clientRequest.GetRequestStream())
                {
                    byte[] byte1 = (new UTF8Encoding()).GetBytes(postdata);
                    requestStream.Write(byte1, 0, byte1.Length);
                }
            }

            response = clientRequest.GetResponse() as HttpWebResponse;
        }
        catch (WebException wex)
        {
            Debug.WriteLine($"! Resp::WebException: {wex.Message}");
            if (wex.Response != null)
            {
                response = (HttpWebResponse)wex.Response;  // Get Response
            }
        }
        catch (Exception ex)
        {
            // error occured
            Debug.WriteLine($"! Resp::Exception: {ex.Message}");
        }
        return response;
    }

    private static string BuldPostData(Dictionary<string, string> parameters, HttpWebRequest clientRequest)
    {
        // unique for each call...
        var boundary = $"MIME_{Guid.NewGuid().ToString("N")}";
        var boundaryMarker = $"{seperatorLine}{boundary}";
        var boundaryTerminationMarker = $"{boundaryMarker}{terminateMarker}";

        clientRequest.ContentType = $"multipart/form-data; boundary=\"{boundary}\"";

        var sb = new StringBuilder();

        foreach (var parameter in parameters)
        {
            sb.AppendLine(boundaryMarker)
                .AppendLine("Content-Type: text/plain; charset=utf-8")
                .Append("Content-Disposition: form-data; name=\"").Append(parameter.Key).AppendLine("\"")
                .AppendLine()
                .AppendLine(WebUtility.HtmlEncode(parameter.Value));
        }
        sb.AppendLine(boundaryTerminationMarker);
        return sb.ToString();
    }
}

The controller:
C#
public class ValuesController : ApiController
{
    [HttpGet]
    public HttpResponseMessage GetValues()
    {
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value");
        response.Content = new StringContent("get hello", Encoding.ASCII);
        response.Headers.CacheControl = new CacheControlHeaderValue()
        {
            MaxAge = TimeSpan.FromMinutes(20)
        };
        return response;
    }

    [HttpPost]
    public HttpResponseMessage PostValues()
    {
        if (!Request.Content.IsMimeMultipartContent())
            throw new HttpResponseException(HttpStatusCode.BadRequest);

        var valueField = "** not set **";
        var reqParams = HttpContext.Current.Request.Params;

        if (reqParams.AllKeys.Contains("valueField"))
            valueField = reqParams["valueField"].TrimEnd(new[] { '\r', '\n' });

        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value");
        response.Content = new StringContent($"post hello: [{valueField}]", Encoding.ASCII);
        response.Headers.CacheControl = new CacheControlHeaderValue { MaxAge = TimeSpan.FromMinutes(20) };

        return response;
    }
}

Sample Output:
-- Waiting to begin --

 Good TestGetMethod....

** 200 - OK
[get hello]

Bad TestGetMethod....

** 404 - Not Found
[{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:65189/api/valuesx'.","MessageDetail":"No type was found that matches the controller named 'valuesx'."}]


-- Waiting to begin --

 Good TestPostMethod....

** 200 - OK
[post hello: [test post value]]

Bad TestPostMethod....

** 411 - Length Required
[<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Length Required</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Length Required</h2>
<hr><p>HTTP Error 411. The request must be chunked or have a content length.</p>
</BODY></HTML>
]


-- DONE --

Yes, this does not do binary file uploads, but it is a sound base for you to better understand how it works. Once you understand this example, next have a look at this link for how to do File uploads & Web API controller handling: Sending HTML Form Data in ASP.NET Web API: File Upload and Multipart MIME | Microsoft Docs[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900