I am trying to POST some JSON data to my Server. I am using simple code which I have attained from: Http Post for Windows Phone 8
Code:
string url = "myserver.com/path/to/my/post";
// HTTP web request
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "text/plain; charset=utf-8";
httpWebRequest.Method = "POST";
// Write the request Asynchronously
using (var stream = await Task.Factory.FromAsync<Stream>(httpWebRequest.BeginGetRequestStream,
httpWebRequest.EndGetRequestStream, null))
{
//create some json string
string json = "{ \"my\" : \"json\" }";
// convert json to byte array
byte[] jsonAsBytes = Encoding.UTF8.GetBytes(json);
// Write the bytes to the stream
await stream.WriteAsync(jsonAsBytes, 0, jsonAsBytes.Length);
}
I am getting an error though on await and Task:
Anyone see the obvious error?
Fixed it by changing method signature to:
public async Task ReportSighting(Sighting sighting)
Related
I'm getting the json data from the API call but its not displaying the complete data.There are a lot of properties to work with and creating the model would be difficult as it's my first time working on any API.So i wanted to use app.quicktype.io to parse the json data directly into dart code for which i need to get the complete json data.Right now only little bit of the json data is being displayed in the console.
CODE:
Future<void> getContacts() async {
var client = http.Client();
String contacts_url =
'https://mylinkexample.com';
String basicAuth =
'Basic mykeyexampele';
var response = await client.get(contacts_url,
headers: <String, String>{'authorization': basicAuth});
var jsonString = jsonDecode(response.body);
print(response.statusCode);
print(jsonString);
}
Use this :
import 'dart:developer' as developer;
test() {
developer.log(response.body);
}
public static async Task<string> MakeSlackRequest(string message)
{
var urlJsonData = "{'text':'message'}"; // I want my url with json data to convert into jsonstring
using(var client = new HttpClient())
{
var requestData = new StringContent("" + urlJsonData.ToString(), Encoding.UTF8, "application/json");
var response = await client.PostAsync($"webhookURL", requestData);
var result = await response.Content.ReadAsStringAsync();
return result;
}
}
I have a url file(jsonData.json and jsonData.txt), anyone know how to convert a url file with jsonData.json or jsonData.txt into a string for azure function?
Use this code
var urlJsonData = client.GetStringAsync("url/sample.json").Result;
i am sending the data through postman to asp.net webapi. But i am getting the exception.Need to send both file and json data.
Get the json format also in C# coding as string.
public JsonResult FileUploadDetails(HttpPostedFileBase[] Files,string request)
{
//Stream req = Request.InputStream;
//string reqparm = Request.QueryString["request"];
//req.Seek(0, System.IO.SeekOrigin.Begin);
//var jsonStr = new StreamReader(reqparm).ReadToEnd();
JObject obj = JObject.Parse(request);
.................
...............
}
I am working on a web-service application using JSON.
In performing task I got success in directly fetching JSOn response by hitting the URL.
Now I have a task to request with a request parameter.
enter code here
private void callJSON_Webservice(String method,String paraLastModifiedDate) {
HttpConnection c=null;
InputStream is = null;
String feedURL = Constants.feedURL;
int rc;
try{
JSONObject postObject = new JSONObject();
postObject.put("CheckLatestDataDate",method);
postObject.put("LastModifiedDate", paraLastModifiedDate);
//c = new HttpConnectionFactory().getHttpConnection(feedURL);
c = (HttpConnection)Connector.open(feedURL + ConnectionManager.getConnectionString());
// Set the request method and headers
c.setRequestMethod(HttpConnection.GET);
c.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
c.setRequestProperty("Content-Length", "" + (postObject.toString().length() - 2));
//c.setRequestProperty("method", HttpConnection.GET);
// Getting the response code will open the connection,
// send the request, and read the HTTP response headers.
// The headers are stored until requested.
rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK){
throw new IOException("HTTP response code: " + rc);
}
is = c.openInputStream();
String json = StringUtils.convertStreamToString(is);
object = new JSONObject(json);
}catch (Exception e) {
System.out.println(e+"call webservice exception");
}
}
With this code I am getting EOF exception. I need to complete this small task as soon as possible. Please help me...!
Thanx in advance
Try replacing
is = c.openInputStream();
String json = StringUtils.convertStreamToString(is);
with following:
is = c.openInputStream();
StringBuffer buffer = new StringBuffer();
int ch = 0;
while (ch != -1) {
ch = is.read();
buffer.append((char) ch);
}
String json = buffer.toString();
Reference: convert StreamConnection to String
I am trying to extract Digg data for a user using this URL:
"http://services.digg.com/user/vamsivanka/diggs?count=25&appkey=34asd56asdf789as87df65s4fas6"
and the web response is throwing an error "The remote server returned an error: (403) Forbidden."
Please let me know.
public static XmlTextReader CreateWebRequest(string url)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.UserAgent = ".NET Framework digg Test Client";
webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
webRequest.Accept = "text/xml";
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
System.IO.Stream responseStream = webResponse.GetResponseStream();
XmlTextReader reader = new XmlTextReader(responseStream);
return reader;
}