Upload a file via POST using RestSharp
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
add a comment |
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
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
add a comment |
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
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
c# post restsharp
edited Jun 16 '14 at 11:14
chridam
66.4k13105138
66.4k13105138
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
Is NEVER.jpg in the same location as the exe? Because the way you're calling AddFile assumes it is.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Is NEVER.jpg in the same location as the exe? Because the way you're calling AddFile assumes it is.
add a comment |
Is NEVER.jpg in the same location as the exe? Because the way you're calling AddFile assumes it is.
add a comment |
Is NEVER.jpg in the same location as the exe? Because the way you're calling AddFile assumes it is.
Is NEVER.jpg in the same location as the exe? Because the way you're calling AddFile assumes it is.
answered Jan 5 '15 at 19:25
TheKrushTheKrush
316
316
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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