这是我第一次在这里问一个问题(我来自亚洲)。
平台:UWP 17632
IDE:Visual Studio 2017
基于项目的重新设计,我需要将一些信息发布到网站上。
我将介绍有关How to make HTTP POST web request方法A的答案。
这是我的代码:
public async void PostDataAsync(string pTemperture, string pHumidity, string pFireStatus, string pLightStatus, string pBodyStatus)
{
var values = new Dictionary<string, string>
{
{"count", "1" },
{"temperture_0", pTemperture },
{"Humidity_0", pHumidity },
{"FireStatus_0", pFireStatus },
{"LightStatus_0" ,pLightStatus},
{"BodyDetect_0", pBodyStatus }
};
var content = new FormUrlEncodedContent(values);
try
{
var response = await client.PostAsync("http://115.159.36.210/api/onehome/upload", content);//Here throw an exception
System.Diagnostics.Debug.WriteLine(response);
var responseString = response.Content.ReadAsStringAsync();
System.Diagnostics.Debug.WriteLine(responseString);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.HelpLink);
System.Diagnostics.Debug.WriteLine(ex.Message);
throw;
}
}
然后抛出异常
“An error occurred while sending the request.”
在
var response = await client.PostAsync("http://115.159.36.210/api/onehome/upload", content);
我想知道为什么并获得可以解决它的解决方案。
如果您能帮助我,我将不胜感激。
答案 0 :(得分:2)
建议使用HttpClient
和其余Windows.Web.Http
名称空间API使用UWP中的HTTP 2.0和HTTP 1.1协议发送和接收信息。
根据您的要求,您可以像下面这样创建打包http POST方法的方法
public async void SendPostMethod(string url, Dictionary<string, string> param, Action<string> response)
{
HttpClient client = new HttpClient();
HttpResponseMessage httpResponse = new HttpResponseMessage();
Uri requestUri = new Uri(url);
var content = new HttpFormUrlEncodedContent(param);
try
{
httpResponse = await client.PostAsync(requestUri, content);
response(await httpResponse.Content.ReadAsStringAsync());
}
catch (Exception ex)
{
}
}
用法
this.SendPostMethod("http://115.159.36.210/api/onehome/upload",Param, (res) =>
{
var response = res;
});
您可以参考官方的code sample和document。
答案 1 :(得分:2)
我是服务器的作者。
现实是我还没有完成服务器的代码。
因此, from tkinter import *
from tkinter.ttk import *
from tkinter.ttk import Progressbar
from tkinter import ttk
import tkinter as tk
import tkinter.ttk, threading
tkk = Tk()
tkk.title("sample")
tkk.geometry('350x200')
number= tk.StringVar()
choice= Combobox(tkk, width=12, textvariable=number)
choice.grid(column=0,row=1)
class progress():
def __init__(self, parent):
self.progressbar = ttk.Progressbar(tkk, orient = HORIZONTAL, mode = 'indeterminate')
self.t = threading.Thread()
self.t.__init__(target = self.progressbar.start, args = ())
self.progressbar.grid(row=4,columnspan=6,sticky=W+E,padx=(40,10))
self.t.start()
def end(self):
self.progressbar.stop()
self.t.join()
def click():
action.configure(text="chosen case is : "+ selected.get())
print("clicked button is :")
id = 0
while id < 10:
progress(tkk)
id+=1
else:
new=progress(tkk)
new.end()
action = ttk.Button(tkk, text="Click", command=click)
action.grid(column=1,row=1)
selected= ttk.Combobox(tkk, width=12, textvariable=number)
selected['values']=('a','b','c','d')
selected.grid(column=0,row=1)
selected.current()
tkk.mainloop()
是默认结果.....