Upload a file via POST using RestSharp












0















I'm trying to simulate a simple multipart HTML form in C#.



I was successful posting text as parameters using the addParameter function.
Now I want to upload a file using POST. I tried using the addFile function of the RestRequest object. But it did not work! I'm getting response code: 0.



Here's my code:



//preparing RestRequest by adding server url, parameteres and files...
RestRequest request = new RestRequest("http://" + ipTextBox.Text + "/samplepost/postdata.php", Method.POST);

request.AddParameter("msgpost", msgTextBox.Text);
request.AddFile("file1", "NEVER.jpg");

//calling server with restClient
RestClient restClient = new RestClient();
restClient.ExecuteAsync(request, (response) =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
//POST successful
MessageBox.Show("Success!");
}
else
{
//error ocured during POST
MessageBox.Show(":-(nFailed.nError: " + response.ErrorMessage);
}
});


Please help me find mistakes in my code.
Thanks in advance!










share|improve this question

























  • As this question has nothing to do with WPF, I have removed the wpf tag.

    – Sheridan
    Jun 16 '14 at 11:11











  • Possible duplicate of Upload File Without Multipart/Form-Data Using RestSharp

    – Jonas M.
    Nov 23 '18 at 13:10
















0















I'm trying to simulate a simple multipart HTML form in C#.



I was successful posting text as parameters using the addParameter function.
Now I want to upload a file using POST. I tried using the addFile function of the RestRequest object. But it did not work! I'm getting response code: 0.



Here's my code:



//preparing RestRequest by adding server url, parameteres and files...
RestRequest request = new RestRequest("http://" + ipTextBox.Text + "/samplepost/postdata.php", Method.POST);

request.AddParameter("msgpost", msgTextBox.Text);
request.AddFile("file1", "NEVER.jpg");

//calling server with restClient
RestClient restClient = new RestClient();
restClient.ExecuteAsync(request, (response) =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
//POST successful
MessageBox.Show("Success!");
}
else
{
//error ocured during POST
MessageBox.Show(":-(nFailed.nError: " + response.ErrorMessage);
}
});


Please help me find mistakes in my code.
Thanks in advance!










share|improve this question

























  • As this question has nothing to do with WPF, I have removed the wpf tag.

    – Sheridan
    Jun 16 '14 at 11:11











  • Possible duplicate of Upload File Without Multipart/Form-Data Using RestSharp

    – Jonas M.
    Nov 23 '18 at 13:10














0












0








0


0






I'm trying to simulate a simple multipart HTML form in C#.



I was successful posting text as parameters using the addParameter function.
Now I want to upload a file using POST. I tried using the addFile function of the RestRequest object. But it did not work! I'm getting response code: 0.



Here's my code:



//preparing RestRequest by adding server url, parameteres and files...
RestRequest request = new RestRequest("http://" + ipTextBox.Text + "/samplepost/postdata.php", Method.POST);

request.AddParameter("msgpost", msgTextBox.Text);
request.AddFile("file1", "NEVER.jpg");

//calling server with restClient
RestClient restClient = new RestClient();
restClient.ExecuteAsync(request, (response) =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
//POST successful
MessageBox.Show("Success!");
}
else
{
//error ocured during POST
MessageBox.Show(":-(nFailed.nError: " + response.ErrorMessage);
}
});


Please help me find mistakes in my code.
Thanks in advance!










share|improve this question
















I'm trying to simulate a simple multipart HTML form in C#.



I was successful posting text as parameters using the addParameter function.
Now I want to upload a file using POST. I tried using the addFile function of the RestRequest object. But it did not work! I'm getting response code: 0.



Here's my code:



//preparing RestRequest by adding server url, parameteres and files...
RestRequest request = new RestRequest("http://" + ipTextBox.Text + "/samplepost/postdata.php", Method.POST);

request.AddParameter("msgpost", msgTextBox.Text);
request.AddFile("file1", "NEVER.jpg");

//calling server with restClient
RestClient restClient = new RestClient();
restClient.ExecuteAsync(request, (response) =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
//POST successful
MessageBox.Show("Success!");
}
else
{
//error ocured during POST
MessageBox.Show(":-(nFailed.nError: " + response.ErrorMessage);
}
});


Please help me find mistakes in my code.
Thanks in advance!







c# post restsharp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 16 '14 at 11:14









chridam

68.7k16115149




68.7k16115149










asked Jun 16 '14 at 11:07









Shrayansh SharmaShrayansh Sharma

102415




102415













  • As this question has nothing to do with WPF, I have removed the wpf tag.

    – Sheridan
    Jun 16 '14 at 11:11











  • Possible duplicate of Upload File Without Multipart/Form-Data Using RestSharp

    – Jonas M.
    Nov 23 '18 at 13:10



















  • As this question has nothing to do with WPF, I have removed the wpf tag.

    – Sheridan
    Jun 16 '14 at 11:11











  • Possible duplicate of Upload File Without Multipart/Form-Data Using RestSharp

    – Jonas M.
    Nov 23 '18 at 13:10

















As this question has nothing to do with WPF, I have removed the wpf tag.

– Sheridan
Jun 16 '14 at 11:11





As this question has nothing to do with WPF, I have removed the wpf tag.

– Sheridan
Jun 16 '14 at 11:11













Possible duplicate of Upload File Without Multipart/Form-Data Using RestSharp

– Jonas M.
Nov 23 '18 at 13:10





Possible duplicate of Upload File Without Multipart/Form-Data Using RestSharp

– Jonas M.
Nov 23 '18 at 13:10












1 Answer
1






active

oldest

votes


















0














Is NEVER.jpg in the same location as the exe? Because the way you're calling AddFile assumes it is.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24242302%2fupload-a-file-via-post-using-restsharp%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Is NEVER.jpg in the same location as the exe? Because the way you're calling AddFile assumes it is.






    share|improve this answer




























      0














      Is NEVER.jpg in the same location as the exe? Because the way you're calling AddFile assumes it is.






      share|improve this answer


























        0












        0








        0







        Is NEVER.jpg in the same location as the exe? Because the way you're calling AddFile assumes it is.






        share|improve this answer













        Is NEVER.jpg in the same location as the exe? Because the way you're calling AddFile assumes it is.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 5 '15 at 19:25









        TheKrushTheKrush

        316




        316
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24242302%2fupload-a-file-via-post-using-restsharp%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini