How to send an array of strings using WebRequest - c#

I have a method in my controller that accepts an array of strings:
public async Task<HttpResponseMessage> PostData(string[] data)
The thing I want to send a request using a console app to my api, but Im getting a 404 response and its not reaching the controller:
WebRequest request = WebRequest.Create("http://localhost:1119/api/psr");
request.Method = "POST";
string num= "[ \"89\",\"21\" , \"2A,\" ]";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(num);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(data, 0, data.Length);
dataStream.Close();
WebResponse response = request.GetResponse();

Related

get response header of request from HttpWebResponse

i am trying to implement my code as per these requirement. That is saying The response to this POST will include a header named LOCATION, which is a URL with parameters indicating where to upload the file via a second POST.. For request i have did below code but i stuck at point where i need to get LOCATION from header. For request i did below code and i want to get LOCATION from Header of response.
string responseStr = "";
HttpStatusCode statusCode;
WebRequest request = WebRequest.Create(URL);
//request.Credentials = new NetworkCredential(bigApiUserID, BigApiKey);
request.Method = "POST";
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(stringOrder);
request.Headers.Add("Authentication", token);
request.ContentType = "application/json";
request.ContentLength = bytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
statusCode = response.StatusCode;
response.Headers
if (Convert.ToString(response.StatusCode) == "OK")
{
Stream responseStream = response.GetResponseStream();
responseStr = new StreamReader(responseStream).ReadToEnd();
return responseStr;
}

API call going in timeout from windows forms working fine from Postman

I am trying to call API from windows forms its going in timeout. but its working fine from POSTMAN app.
I am using below code for calling web API from windows app.
public string ReadXMLResponse(string strrequestxml, string strTallyServer1)
{
string URL = strTallyServer1;
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
myHttpWebRequest.Accept = "application/xml";
myHttpWebRequest.ContentType = "application/xml";
myHttpWebRequest.Timeout = 60000;
string method = "POST";
myHttpWebRequest.Method = method;
if (method == "POST")
{
using (var streamWriter = new StreamWriter(myHttpWebRequest.GetRequestStream()))
{
streamWriter.Write(strrequestxml);
streamWriter.Flush();
}
}
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
using (var streamReader = new StreamReader(myHttpWebResponse.GetResponseStream()))
{
var streamRead = streamReader.ReadToEnd().Trim();
return streamRead;
}
return "";
}
I had the same code and the same problem. It was driving me crazy till I found something on one of Microsoft blogs. Tried to google the original page but I couldn't find it.
WebRequest request = WebRequest.Create("URL");
request.Method = "POST";
var postData = string.Format(dataFormnat, Uri.EscapeDataString(data.Serialize()));
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();

The remote server returned an error: (403) Forbidden.. in google short url

public string GetShortURL(string longUrl)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.googleapis.com/urlshortener/v1/url?key=My_API_Key");
request.Method = "POST";
request.ContentType = "application/json";
string requestData = string.Format(#"{{""longUrl"": ""{0}""}}", longUrl);
byte[] requestRawData = Encoding.ASCII.GetBytes(requestData);
request.ContentLength = requestRawData.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(requestRawData, 0, requestRawData.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader responseReader = new StreamReader(response.GetResponseStream());
string responseData = responseReader.ReadToEnd();
responseReader.Close();
return responseData;
}
I am trying to create short url by passing my original url string but it is giving an exception

(400) Bad Request, can't get response (Custom minecraft launcher)

I just can't seem to be able to post a JSON to the webpage https://authserver.mojang.com/authenticate and get a response.
when I post the JSON it just says
The remote server returned an error: (400) Bad Request
I've gone through many different scripts by others and created my own by converting the Java code to C#. Anyway, here's the code that has worked the best so far.
string authserver = "https://authserver.mojang.com/authenticate";
byte[] rawData = fs.GetBytes(**[JSON]**);
WebRequest request = WebRequest.Create(authserver);
request.ContentType = "application/json";
request.Method = "POST";
//request.ContentLength = rawData.LongLength;
WebResponse connection = request.GetResponse();
connection.ContentType = "application/json";
connection.ContentLength = rawData.LongLength;
Stream stream = connection.GetResponseStream();
stream.Write(rawData, 0, rawData.Length);
stream.Flush();
byte[] rawVerification = new byte[10000];
int count = stream.Read(rawVerification, 0, 10000);
Edit:
is it possible to do this code with webclient?
Edit:
it had an invalid input, the json didn't have the correct data needed
try this:
WebRequest request = WebRequest.Create (authserver);
request.Method = "POST";
string postData = "YourJSON";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
request.ContentType = "application/json";
request.ContentLength = byteArray.Length;
using(Stream s = request.GetRequestStream ()){
s.Write (byteArray, 0, byteArray.Length);
}
WebResponse response = request.GetResponse ();
using(var dataStream = response.GetResponseStream ()){
using(StreamReader reader = new StreamReader (dataStream)){
string responseFromServer = reader.ReadToEnd ();
}
}
response.Close();
Essentially You shouldn't call getResponse() before you submit your data

post request with C# using http and .NET 5.0

http://i.stack.imgur.com/hDpwl.jpg
I need a code snipped which I can use in a WPF application. It should send a post request to the server and return a JSON. Best would be if its working same as the print screen from chrome add-on postman above. I tried to use uploading form Data with HttpClient and uploading form data with WebRequest and it returned HTML page with an error.
Thanks all
public string PostJson(string url,string json)
{
WebRequest request = WebRequest.Create(url);
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(json);
request.ContentType = "application/json";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.Writeline(string.Format("Request {0} Response Code {1} Resonse Status Description {2} Response From Server {3}",
json, ((HttpWebResponse)response).StatusCode,
((HttpWebResponse)response).StatusDescription, responseFromServer));
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}

Categories